Ruby on rails 在rails中查找联接表时出现问题
我有模型表示关联规则(Body=>Head) 我想找到指定了主体项匹配项的规则,并希望通过规则访问其头部,但我不能这样做Ruby on rails 在rails中查找联接表时出现问题,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我有模型表示关联规则(Body=>Head) 我想找到指定了主体项匹配项的规则,并希望通过规则访问其头部,但我不能这样做 def Rule has_many :heads has_many :bodies has_many :item, :through => :heads has_many :item, :through => :bodies ... end 为了实现这一目标,我应该做些什么 谢谢 def Rule has_many :heads has_many :bo
def Rule
has_many :heads
has_many :bodies
has_many :item, :through => :heads
has_many :item, :through => :bodies
...
end
为了实现这一目标,我应该做些什么
谢谢
def Rule
has_many :heads
has_many :bodies
has_many :head_items, :through => :heads
has_many :body_items, :through => :bodies
...
end
你需要为每一种方法建立不同的关联。最后我提出了这个解决方案
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
不确定这是否是一个好的解决方案,但这就是我现在使用的。我应该使用head\u items和body\u items模型作为项目的继承?
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