Uninitialized constant exception when trying to navigate a has_many => through relationship

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:incompute_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:inklass’
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:inload_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:ininspect’
from c:/ruby/lib/ruby/1.8/irb.rb:298:in output_value' from c:/ruby/lib/ruby/1.8/irb.rb:151:ineval_input’
from c:/ruby/lib/ruby/1.8/irb.rb:259:in signal_status' from c:/ruby/lib/ruby/1.8/irb.rb:147:ineval_input’
from c:/ruby/lib/ruby/1.8/irb.rb:146:in eval_input' from c:/ruby/lib/ruby/1.8/irb.rb:70:instart’
from c:/ruby/lib/ruby/1.8/irb.rb:69:in catch' from c:/ruby/lib/ruby/1.8/irb.rb:69:instart’
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

belongs_to’s should be singular.

It should belong_to :project. Reads like “belongs to a project” instead
of
“belongs to a projects”

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

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: [email protected]
[mailto:[email protected]] On Behalf Of Ryan B.
(Radar)
Sent: Tuesday, May 13, 2008 3:39 PM
To: [email protected]
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”

Ah, quite right–thanks!


From: [email protected]
[mailto:[email protected]] On Behalf Of Ryan B.
(Radar)
Sent: Tuesday, May 13, 2008 3:39 PM
To: [email protected]
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”

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs