Reputation: 4502
I have a has_many_through relationship and am getting an "undefined local variable" exception when I pull up the localhost:300/lists.
I have Listables - which are made up of Lists, Sources and Elements.
Listables Model:
class Listable < ActiveRecord::Base
belongs_to :lists
belongs_to :sources
belongs_to :elements
end
Source Model:
class Source < ActiveRecord::Base
has_many :listables
has_many :lists, through => :listables
has_many :elements, through => :listables
end
Elements Model:
class Element < ActiveRecord::Base
has_many :listables
has_many :lists, through => :listables
has_many :sources, through => :listables
end
Lists Model:
class List < ActiveRecord::Base
has_many :listables
has_many :sources, through => :listables
has_many :elements, through => :listables
end
Upvotes: 0
Views: 84
Reputation: 4502
The lines has_many :sources, through => :listables need a : before the through. So it should read:
has_many :sources, :through => :listables
Upvotes: 0
Reputation: 16769
The name of the type after belongs_to
should be singular, not plural as you have it. E.g. try this:
class Listable < ActiveRecord::Base
belongs_to :list
belongs_to :source
belongs_to :element
end
See http://guides.rubyonrails.org/association_basics.html#choosing-between-belongs-to-and-has-one
Upvotes: 2