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
September 8th, 2010 at 11:35
helped me, thanks
September 10th, 2010 at 23:50
Woo hoo! Your writeup is spot-on – thanks!
November 13th, 2010 at 14:44
Thank you very much! I have been following along a training video (Lynda.com) and had this problem. Thanks!
November 26th, 2010 at 18:11
one more word of thanks.
December 9th, 2010 at 09:14
A. May. Zing. Thank you so much.
March 10th, 2011 at 14:20
Thank you :)
March 10th, 2011 at 14:46
Mega dittos.
Also a couple of comments:
1. In the Third Edition of “Agile Web Development with Rails” on pp 370 and following, they use the plural form of the table name. Perhaps this was changed in Rails 3?
2. It would seem that rather than throw an ‘INTERNAL ERROR’ rails could try to singularize the name and try again, or at least suggest that a singular ought to be used.
March 11th, 2011 at 15:25
Thanks man. The application was up and running after this.
April 1st, 2011 at 03:20
Your Post Saved My Butt Tonight!
I did not have the same problems, but similar issues. Go figure… It was Rails ‘Naming Convention’ !!!. Thanks a million. Now I can move on.
April 18th, 2011 at 01:02
Man I hate Ruby. I think I’m done with it… too many vague errors. At least in Java/C# you can tell what’s going on.
May 7th, 2011 at 14:49
Small addition: I had a very similar problem in that I was getting this exact error. Mine turned out to be a naming convention problem where I had named the class file of my join table pluralized, but the actual class name was not. In the terms of your example I would have had file name: memberships.rb and class name Membership.
Weirdness. Thanks for the posting nonetheless as it got me looking at pluralization.
May 24th, 2011 at 19:53
Thanks. Much.
July 18th, 2011 at 08:13
Thanks a lot!!! You are my savior!!!!!
October 12th, 2011 at 21:54
I could have spent a LONG time on this. Thanks! Good catch…years later.
January 9th, 2012 at 15:29
WOW!! thanks!!! years later this also saved me!! i spent 3 hours and found this to be my saving grace…
Dam pluralisation!!