我有一个has_many_through关系,当我拉出localhost:300 / lists时,我得到一个“未定义的局部变量”异常。
我有Listables - 由列表,来源和元素组成。
可列表模型:
class Listable < ActiveRecord::Base
belongs_to :lists
belongs_to :sources
belongs_to :elements
end
来源模型:
class Source < ActiveRecord::Base
has_many :listables
has_many :lists, through => :listables
has_many :elements, through => :listables
end
元素模型:
class Element < ActiveRecord::Base
has_many :listables
has_many :lists, through => :listables
has_many :sources, through => :listables
end
列表型号:
class List < ActiveRecord::Base
has_many :listables
has_many :sources, through => :listables
has_many :elements, through => :listables
end
答案 0 :(得分:2)
belongs_to
之后的类型名称应该是单数,而不是复数。例如。试试这个:
class Listable < ActiveRecord::Base
belongs_to :list
belongs_to :source
belongs_to :element
end
请参阅http://guides.rubyonrails.org/association_basics.html#choosing-between-belongs-to-and-has-one
答案 1 :(得分:0)
行has_many:sources,through =&gt; :listables需要一个:在通过之前。所以它应该是:
has_many :sources, :through => :listables