Technology
From the Trenches

Ruby on Rails has_many :through NameError: uninitialized constant

Posted on Monday, March 10th, 2008 at 17:40 MST by Brent Kearney

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.

32 Responses to “Ruby on Rails has_many :through NameError: uninitialized constant”

  1. eirc Says:

    THANK YOU!!!!!!!

    it took me about an hour until i found your post…
    i would surely spend the whole day on this!!

  2. dave Says:

    OMG I <3 you.

  3. Fredrik Says:

    Thanks! Subtle indeed, and almost too obvious to notice.

  4. Edouard Says:

    Thanks! It just helped me out!

  5. dustycoder Says:

    Spent an hour on this… you just saved me another three days, surely.

  6. chris roller Says:

    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.

  7. Roman Says:

    haha, yes, same mistake. Decided to google it after some time of checking all the related code. 1-st line in search results :) Thanks.

  8. Velmurugan Says:

    Thanks finally it is working well…

  9. Daniel Says:

    Thank You buddy ! you saved me a lot of time

  10. lobo_tuerto Says:

    Thank you mate, was stuck with this problem!
    :)

  11. Scott Brown Says:

    Many thanks! Was scratching my head over this one.

  12. hctulk Says:

    finally! haha, thanks

  13. Victor Martins Says:

    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!

  14. Nick Says:

    I should’ve googled this problem sooner… I looked over everything multiple times and couldn’t find the problem. Thank you, that was it!

  15. piyush jha Says:

    Thanks .. I spent two hours debugging the same… finally i googled and found you… thanks a lot !!

  16. samba Says:

    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

  17. mark Says:

    wasted about 45 minutes of my life too :( thanks

  18. sebastian Says:

    helped me, thanks

  19. harold Says:

    Woo hoo! Your writeup is spot-on – thanks!

  20. Jarod Says:

    Thank you very much! I have been following along a training video (Lynda.com) and had this problem. Thanks!

  21. Kevin Says:

    one more word of thanks.

  22. Chris Says:

    A. May. Zing. Thank you so much.

  23. Snorri Páll Says:

    Thank you :)

  24. Dan Doherty Says:

    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.

  25. RailsNoob Says:

    Thanks man. The application was up and running after this.

  26. eddie Says:

    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.

  27. Richard Says:

    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.

  28. Chris Says:

    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.

  29. anbrie Says:

    Thanks. Much.

  30. Ernest Says:

    Thanks a lot!!! You are my savior!!!!!

  31. Steven Arnold Says:

    I could have spent a LONG time on this. Thanks! Good catch…years later.

  32. Will Says:

    WOW!! thanks!!! years later this also saved me!! i spent 3 hours and found this to be my saving grace…

    Dam pluralisation!!

Leave a Reply

Live Comment Preview: