Technology
From the Trenches

Ruby on Rails has_many :through NameError: uninitialized constant

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

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.

17 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

Leave a Reply

Live Comment Preview: