# Uninitialized constant exception when trying to navigate a has\_many =\> through relationship

**URL:** <https://www.ruby-forum.com/t/uninitialized-constant-exception-when-trying-to-navigate-a-has-many-through-relationship/137530>\
**Category:** Rails\
**Created:** [May 14, 2008, 12:04am UTC](https://www.ruby-forum.com/t/uninitialized-constant-exception-when-trying-to-navigate-a-has-many-through-relationship/137530 "2008-05-14T00:04:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rpardee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rpardee](https://www.ruby-forum.com/u/rpardee)\
**Post date:** [May 14, 2008, 12:04am UTC](https://www.ruby-forum.com/t/uninitialized-constant-exception-when-trying-to-navigate-a-has-many-through-relationship/137530/1 "2008-05-14T00:04:19Z")

</div>

Hey All,

I’m trying to do a many-to-many relationship w/has\_many, :through and  
having trouble.

I want to classify my Projects into one or more ResearchAreas. So I  
have a Project model, a ResearchArea model, and a ProjectsResearchArea  
model to link them. So–many-to-many between Projects & ResearchAreas.  
I’m not sure where I went wrong, but when I try to traverse the hm=\>t  
relationship (from either Project or ResearchArea) I get “NameError:  
uninitialized constant ResearchArea::Projects” (see below for details).  
However, I can traverse the simple has\_many relationship to my join  
model (illustrated below).

Can anybody tell me what I’ve done wrong?

Thanks!

-Roy

Migration:

create\_table :research\_areas, :force =\> true do |t|  
t.string :name, :null =\> false  
t.string :abbreviation  
t.timestamps  
end  
create\_table :projects\_research\_areas, :force =\> true do |t|  
t.integer :project\_id  
t.integer :research\_area\_id  
end

Models:

class ProjectsResearchArea \< ActiveRecord::Base  
belongs\_to :projects  
belongs\_to :research\_areas  
end

class ResearchArea \< ActiveRecord::Base  
has\_many :projects\_research\_areas, :dependent =\> :delete\_all  
has\_many :projects, :through =\> :projects\_research\_areas  
end

class Project \< ActiveRecord::Base  
has\_many :projects\_research\_areas, :dependent =\> :delete\_all  
has\_many :research\_areas, :through =\> :projects\_research\_areas  
end

Console session:

C:\railsapps\collabtrac\>ruby script\console  
Loading development environment (Rails 2.0.2)

> > ra = ResearchArea.find(:first)  
> > =\> #\<ResearchArea id: 20, name: “Behavior”, abbreviation: nil,  
> > created\_at: “2008  
> > -05-13 12:50:50”, updated\_at: “2008-05-13 12:50:50”\>
> 
> > prj = Project.find(:first)  
> > =\> #\<Project id: 2, name: “Adolescent Depression Screening”,  
> > abbreviation: “ASC”  
> > , description: “Tests a brief screening instrument for screening  
> > ad…”, grant\_n  
> > umber: “3”, status\_id: 2, start\_date: “2008-06-27”, end\_date:  
> > “2008-04-29”, fund  
> > er\_id: 3, funding\_mechanism: nil\>
> 
> > ra.projects\_research\_areas.count  
> > =\> 0
> 
> > prj.projects\_research\_areas.count  
> > =\> 0
> 
> > ra.projects  
> > NameError: uninitialized constant ResearchArea::Projects  
> > from  
> > c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active\_suppo  
> > rt/dependencies.rb:478:in `const_missing' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record /base.rb:1750:in`compute\_type’  
> > from  
> > c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active\_record  
> > /reflection.rb:125:in `send' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record /reflection.rb:125:in`klass’  
> > from  
> > c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active\_record  
> > /associations/has\_many\_through\_association.rb:140:in `find_target' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record /associations/association_proxy.rb:133:in`load\_target’  
> > from  
> > c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active\_record  
> > /associations/association\_proxy.rb:55:in `reload' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record /associations/association_proxy.rb:77:in`inspect’  
> > from c:/ruby/lib/ruby/1.8/irb.rb:298:in `output_value' from c:/ruby/lib/ruby/1.8/irb.rb:151:in`eval\_input’  
> > from c:/ruby/lib/ruby/1.8/irb.rb:259:in `signal_status' from c:/ruby/lib/ruby/1.8/irb.rb:147:in`eval\_input’  
> > from c:/ruby/lib/ruby/1.8/irb.rb:146:in `eval_input' from c:/ruby/lib/ruby/1.8/irb.rb:70:in`start’  
> > from c:/ruby/lib/ruby/1.8/irb.rb:69:in `catch' from c:/ruby/lib/ruby/1.8/irb.rb:69:in`start’  
> > from c:/ruby/bin/irb.bat:15\>\>  
> > ?\>

Roy P.  
Research Analyst/Programmer  
Group Health Center For Health Studies (Cancer Research Network)  
(206) 287-2078  
Google Talk: rpardee

---

<div class="post-metadata">

**Author:** ![rpardee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rpardee](https://www.ruby-forum.com/u/rpardee)\
**Post date:** [May 14, 2008, 12:39am UTC](https://www.ruby-forum.com/t/uninitialized-constant-exception-when-trying-to-navigate-a-has-many-through-relationship/137530/2 "2008-05-14T00:39:02Z")

</div>

belongs\_to’s should be singular.

It should belong\_to :project. Reads like “belongs to a project” instead  
of  
“belongs to a projects”

---

<div class="post-metadata">

**Author:** ![rpardee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rpardee](https://www.ruby-forum.com/u/rpardee)\
**Post date:** [May 14, 2008, 2:32am UTC](https://www.ruby-forum.com/t/uninitialized-constant-exception-when-trying-to-navigate-a-has-many-through-relationship/137530/3 "2008-05-14T02:32:13Z")

</div>

Ach–nevermind–figured this out on my ferry ride home. The problem is  
my join model–I was using plural versions of the model names in  
belongs\_to, and they need to be singular. So changing to this:

class ProjectsResearchArea \< ActiveRecord::Base  
belongs\_to :project  
belongs\_to :research\_area  
end

Fixed me up.

Sorry for any wasted brain cycles…

-Roy

---

<div class="post-metadata">

**Author:** ![rpardee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rpardee](https://www.ruby-forum.com/u/rpardee)\
**Post date:** [May 28, 2008, 12:27am UTC](https://www.ruby-forum.com/t/uninitialized-constant-exception-when-trying-to-navigate-a-has-many-through-relationship/137530/4 "2008-05-28T00:27:33Z")

</div>

Just had the same problem and was solved with one quick research. THANKS  
TO THE GREAT COMMUNITY!

Roy P. wrote:

> Ah, quite right–thanks!
> 
> * * *
> 
> From: removed\_email\_address@domain.invalid  
> [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Ryan B.  
> (Radar)  
> Sent: Tuesday, May 13, 2008 3:39 PM  
> To: removed\_email\_address@domain.invalid  
> Subject: [Rails] Re: Uninitialized constant exception when trying to  
> navigate a has\_many =\> through relationship
> 
> belongs\_to’s should be singular.
> 
> It should belong\_to :project. Reads like “belongs to a project” instead  
> of “belongs to a projects”

---

<div class="post-metadata">

**Author:** ![rpardee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rpardee](https://www.ruby-forum.com/u/rpardee)\
**Post date:** [May 14, 2008, 2:32am UTC](https://www.ruby-forum.com/t/uninitialized-constant-exception-when-trying-to-navigate-a-has-many-through-relationship/137530/5 "2008-05-14T02:32:48Z")

</div>

Ah, quite right–thanks!

* * *

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Ryan B.  
(Radar)  
Sent: Tuesday, May 13, 2008 3:39 PM  
To: removed\_email\_address@domain.invalid  
Subject: [Rails] Re: Uninitialized constant exception when trying to  
navigate a has\_many =\> through relationship

belongs\_to’s should be singular.

It should belong\_to :project. Reads like “belongs to a project” instead  
of “belongs to a projects”
