2012-03-24 72 views
1

我有这个在我的架构:如何在Ruby on Rails中使对象成为模型的属性?

create_table "robots_matches", :force => true do |t| 
t.integer "robot_id" 
t.integer "match_id" 

,我想我希望能够从我的robots_match模型中加载一个机器人比赛,所以我可以做这样的事情: robots_match.find(:身份证。).get_robot()名称

我在robots_matches模型的尝试是这样的:

def get_robot 
Robot.find(this.id) 
end 

我超新轨道,所以欢迎随时纠正我的架构决策。

+0

然后,你可以做查询,请出示您的模型。 – 2012-03-24 14:17:03

回答

1

我会考虑从下面的模型开始。
通过'Linker',这可以让一场比赛拥有许多机器人,也可以通过机器人进行多场比赛。
Robot.find(1).matchesMatch.find(1).robots

class Robot < ActiveRecord::Base 
    has_many linkers 
    has_many matches, :through => linkers 

class Linker < ActiveRecord::Base 
    belongs_to :robots 
    belongs_to :matches 

class Match < ActiveRecord::Base 
    has_many linkers 
    has_many robots, :through => linkers