2009-11-15 38 views
0

我有模型代表关联规则(车身=>头)问题查找轨

def Item 
has_many :heads 
has_many :bodies 
... 
end 

def Rule 
has_many :heads 
has_many :bodies 
... 
end 

def Body 
belongs_to :item 
belongs_to :rule 
... 
end 

def Head 
belongs_to :item 
belongs_to :rule 
... 
end 

我想找到具有匹配的指定团体的项目的项目,并希望通过规则来访问它的头部规则,但连接表我不能这样做

def Rule 
has_many :heads 
has_many :bodies 
has_many :item, :through => :heads 
has_many :item, :through => :bodies 
... 
end 

我应该改变什么,并做到这一点?

感谢,

回答

0

最后我想出了这个解决方案

class Rule 
    has_many :heads 
    has_many :bodies 
    ... 
end 
class Body 
    belongs_to :rule 
    has_many :items, :as => :rulable 
end 
class Head 
    belongs_to :rule 
    has_many :items, :as => :rulable 
end 
class Item 
    belongs_to :rulable, :polymorphic => true` 
end 

难道不知道这是一个很好的解决方案,但是这是我使用的现在。

0
def Rule 
    has_many :heads 
    has_many :bodies 
    has_many :head_items, :through => :heads 
    has_many :body_items, :through => :bodies 
    ... 
end 

你需要不同的has_many关联的每个。

+0

我应该有head_items和body_items模型作为项目的继承吗? – sarunw 2009-11-16 16:31:06