I have a table with institutions and a table with projects with I
would like to join twice with a habtm-relation.
***Model institution
has_and_belongs_to_many :projects, :join_table =>
“customerinstitutions”
***Model project
has_and_belongs_to_many :institutions, :join_table =>
“customerinstitutions”
This works fine.
However, I would also like to define a contractor-relationship between
project and institution, so I was trying to redefine the relations
like this:
***Model institution
has_and_belongs_to_many :customerprojects, :class_name =>
“project”, :join_table => “customerinstitutions”
has_and_belongs_to_many :contractorprojects, :class_name =>
“project”, :join_table => “contractorprojects”
***Model project
has_and_belongs_to_many :customerinstitutions, :class_name =>
“institution”, :join_table => “customerinstitutions”
has_and_belongs_to_many :contractorinstitutions, :class_name =>
“institution”, :join_table => “contractorinstitutions”
When I now try to display a project with its customer-institutions, I
get the following error:
***Error
NoMethodError in ProjectsController#show
undefined method institutions' for #<Project:0xb6d709e0> /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/ attribute_methods.rb:205:in
method_missing’
app/controllers/projects_controller.rb:17:in `show’
Any ideas what I am doing wrong? I appreciate any help on this.
update: the error actually reads as follows:
NameError in ProjectsController#show
undefined local variable or method `institution’ for #<Class:
0xb6c1b568>
RAILS_ROOT: /home/christof/Dokumente/ror/v1
Application Trace | Framework Trace | Full Trace
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/
base.rb:1532:in method_missing' /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/ base.rb:1750:in
compute_type’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/
reflection.rb:125:in send' /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/ reflection.rb:125:in
klass’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/
associations/has_and_belongs_to_many_association.rb:132:in
construct_sql' /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/ associations/has_and_belongs_to_many_association.rb:6:in
initialize’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/
associations.rb:1032:in new' /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/ associations.rb:1032:in
customerinstitutions’
app/controllers/projects_controller.rb:17:in `show’
And it looks like the source of the trouble is this line in the
CONTROLLER:
@customerinstitutions =
@project.customerinstitutions.find(:all, :order => “name”)
What I don’t understand: why is rails looking for a method called
“institution”?
On Jan 11, 3:52 pm, kaeptnhaddock [email protected] wrote:
***Error
NoMethodError in ProjectsController#show
undefined method institutions' for #<Project:0xb6d709e0> /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/ attribute_methods.rb:205:in
method_missing’
app/controllers/projects_controller.rb:17:in `show’
Project has customerinstitutions and contractorinstitutions, and #show
is looking for institutions.
///ark
Project has customerinstitutions and contractorinstitutions, and #show
is looking for institutions.
But why? Didn’t I specify in the model as well as in the controller,
that it should be looking for customerinstitutions?
I think the model is fine, for at least I was able to use fixtures
like this one to fill data into both the project-table and the join-
table:
***fixture for project
project_one:
name: someproject
description: blahblah
customerinstitutions: institution_one
contractorinstitutions: institution_two
However, if I remove this line from the controller/show, then the
error doesn’t appear:
@customerinstitutions =
@project.customerinstitutions.find(:all, :order => “name”)
On Jan 12, 12:58 am, kaeptnhaddock [email protected] wrote:
But why? Didn’t I specify in the model as well as in the controller,
that it should be looking for customerinstitutions?
Yep, I see what you mean, and I don’t have a clue what’s going on.
I’d start stepping through that code in the debugger.
///ark