Ruby on Rails has_many :through NameError: uninitialized constant
Are you getting the error message, “NameError: uninitialized constant” from Rails? I was too, and the solution was a simple change to my models. I have two models with “has_many :through”, for my “events” and “people” tables:
class Event < ActiveRecord::Base
has_many :memberships
has_many :people, :through => :memberships
end
And:
class Person < ActiveRecord::Base
has_many :memberships
has_many :events, :through => :memberships
end
The join table is “memberships”, and the problem was in its model:
class Membership < ActiveRecord::Base
validates_presence_of :event_id, :person_id
belongs_to :people
belongs_to :events
end
It is a subtle problem; the model looks OK. However, the "belongs_to" statements should point to the models of the other two tables, not the names of the tables themselves. The models are Event and Person, so the join model should be:
class Membership < ActiveRecord::Base
validates_presence_of :event_id, :person_id
belongs_to :person
belongs_to :event
end
It took me a while to see the problem because it is so subtle, even though the answer is everywhere. Hopefully spelling it out here will help someone.

March 31st, 2008 at 18:56
THANK YOU!!!!!!!
it took me about an hour until i found your post…
i would surely spend the whole day on this!!
May 8th, 2008 at 09:58
OMG I <3 you.
October 2nd, 2008 at 07:07
Thanks! Subtle indeed, and almost too obvious to notice.
October 7th, 2008 at 04:17
Thanks! It just helped me out!
December 21st, 2008 at 22:09
Spent an hour on this… you just saved me another three days, surely.
February 11th, 2009 at 16:10
I found your blog on google and read a few of your other posts. I just added you to my Google News Reader. Keep up the good work. Look forward to reading more from you in the future.
May 31st, 2009 at 09:33
haha, yes, same mistake. Decided to google it after some time of checking all the related code. 1-st line in search results :) Thanks.
June 9th, 2009 at 23:18
Thanks finally it is working well…
July 13th, 2009 at 02:37
Thank You buddy ! you saved me a lot of time
August 10th, 2009 at 16:21
Thank you mate, was stuck with this problem!
:)
December 12th, 2009 at 12:25
Many thanks! Was scratching my head over this one.
December 21st, 2009 at 01:17
finally! haha, thanks
January 6th, 2010 at 09:10
THANKSSSSSSS!!!!
I Was having this problem for so long! Everything was looking ok, I saw many tutorials and everything was similar. EXCEPT THE PLURAL PART! point to the models not the tables! F! I will never forget this one hehe!
January 27th, 2010 at 12:31
I should’ve googled this problem sooner… I looked over everything multiple times and couldn’t find the problem. Thank you, that was it!
February 27th, 2010 at 00:10
Thanks .. I spent two hours debugging the same… finally i googled and found you… thanks a lot !!
March 29th, 2010 at 20:20
i am sorry ! i am program no execute come ;
class Student ruby e4_3.rb
e4_3.rb:3: uninitialized constant Person (NameError)
>Exit code: 1
like extends Person
of problem
May 17th, 2010 at 12:55
wasted about 45 minutes of my life too :( thanks