# How to declare several HABTM-relationships?

**URL:** <https://www.ruby-forum.com/t/how-to-declare-several-habtm-relationships/60328>\
**Category:** Rails\
**Created:** [May 12, 2006, 5:25am UTC](https://www.ruby-forum.com/t/how-to-declare-several-habtm-relationships/60328 "2006-05-12T05:25:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bonicon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f08c70/32.png) [@Bonicon](https://www.ruby-forum.com/u/Bonicon)\
**Post date:** [May 12, 2006, 5:25am UTC](https://www.ruby-forum.com/t/how-to-declare-several-habtm-relationships/60328/1 "2006-05-12T05:25:03Z")

</div>

Hello,

Is it possible to have several HABTM-relationships for the same object?

Example: Imagine I have 3 objects (classroom, student, teacher) and  
define the associations for the student as:

class Student \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :classrooms  
has\_and\_belongs\_to\_many :teachers

class Classroom \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :students

class Teacher \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :students

When I create a new student I have checkboxes to display and assign the  
student to a set of classrooms and teachers. This seems to work, the  
student is saved but only one join-table (classrooms\_students) is  
populated with the relation. The students\_teachers table is not  
populated. Changing the order of the HABTM-declarations will reverse the  
problem (ie, only the first declaration works).

Is it not possible to declare several relations like this, have I just  
screwed up the syntax or committed some far greater sin in how I  
structured the database?

---

<div class="post-metadata">

**Author:** ![Bonicon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f08c70/32.png) [@Bonicon](https://www.ruby-forum.com/u/Bonicon)\
**Post date:** [May 12, 2006, 7:55am UTC](https://www.ruby-forum.com/t/how-to-declare-several-habtm-relationships/60328/2 "2006-05-12T07:55:44Z")

</div>

Bonicon wrote:

> When I create a new student I have checkboxes to display and assign the  
> student to a set of classrooms and teachers. This seems to work, the  
> student is saved but only one join-table (classrooms\_students) is  
> populated with the relation. The students\_teachers table is not  
> populated. Changing the order of the HABTM-declarations will reverse the  
> problem (ie, only the first declaration works).

This was a bug in Rails 1.0. If you’re using it you’ll have to upgrade  
to 1.1.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![Bonicon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f08c70/32.png) [@Bonicon](https://www.ruby-forum.com/u/Bonicon)\
**Post date:** [May 12, 2006, 9:58am UTC](https://www.ruby-forum.com/t/how-to-declare-several-habtm-relationships/60328/3 "2006-05-12T09:58:05Z")

</div>

Thanks, I upgraded to 1.1 and it is working as it should now.
