Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在rails中查找联接表时出现问题_Ruby On Rails_Activerecord - Fatal编程技术网

Ruby on rails 在rails中查找联接表时出现问题

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

我有模型表示关联规则(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 :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