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
=> 0prj.projects_research_areas.count
=> 0ra.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:inconst_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:insend' 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:infind_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:inreload' 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:inoutput_value' from c:/ruby/lib/ruby/1.8/irb.rb:151:in
eval_input’
from c:/ruby/lib/ruby/1.8/irb.rb:259:insignal_status' from c:/ruby/lib/ruby/1.8/irb.rb:147:in
eval_input’
from c:/ruby/lib/ruby/1.8/irb.rb:146:ineval_input' from c:/ruby/lib/ruby/1.8/irb.rb:70:in
start’
from c:/ruby/lib/ruby/1.8/irb.rb:69:incatch' 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