# Multiple habtm-join between tables

**URL:** <https://www.ruby-forum.com/t/multiple-habtm-join-between-tables/125782>\
**Category:** Rails\
**Created:** [January 12, 2008, 12:53am UTC](https://www.ruby-forum.com/t/multiple-habtm-join-between-tables/125782 "2008-01-12T00:53:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kaeptnhaddock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/d26b3c/32.png) [@kaeptnhaddock](https://www.ruby-forum.com/u/kaeptnhaddock)\
**Post date:** [January 12, 2008, 12:53am UTC](https://www.ruby-forum.com/t/multiple-habtm-join-between-tables/125782/1 "2008-01-12T00:53:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kaeptnhaddock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/d26b3c/32.png) [@kaeptnhaddock](https://www.ruby-forum.com/u/kaeptnhaddock)\
**Post date:** [January 12, 2008, 1:54am UTC](https://www.ruby-forum.com/t/multiple-habtm-join-between-tables/125782/2 "2008-01-12T01:54:31Z")

</div>

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”?

---

<div class="post-metadata">

**Author:** ![kaeptnhaddock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/d26b3c/32.png) [@kaeptnhaddock](https://www.ruby-forum.com/u/kaeptnhaddock)\
**Post date:** [January 12, 2008, 1:55am UTC](https://www.ruby-forum.com/t/multiple-habtm-join-between-tables/125782/3 "2008-01-12T01:55:06Z")

</div>

On Jan 11, 3:52 pm, kaeptnhaddock [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![kaeptnhaddock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/d26b3c/32.png) [@kaeptnhaddock](https://www.ruby-forum.com/u/kaeptnhaddock)\
**Post date:** [January 12, 2008, 9:59am UTC](https://www.ruby-forum.com/t/multiple-habtm-join-between-tables/125782/4 "2008-01-12T09:59:04Z")

</div>

> 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”)

---

<div class="post-metadata">

**Author:** ![kaeptnhaddock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/d26b3c/32.png) [@kaeptnhaddock](https://www.ruby-forum.com/u/kaeptnhaddock)\
**Post date:** [January 14, 2008, 9:14pm UTC](https://www.ruby-forum.com/t/multiple-habtm-join-between-tables/125782/5 "2008-01-14T21:14:25Z")

</div>

On Jan 12, 12:58 am, kaeptnhaddock [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
