william tell
william tell

Reputation: 4502

Rails has_many_through undefined local variable

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

Answers (2)

william tell
william tell

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

Dogweather
Dogweather

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

Related Questions