Forum: Ruby on Rails How to declare several HABTM-relationships?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F5c01ba0de38c0566ced81a688a731b6?d=identicon&s=25 Bonicon (Guest)
on 2006-05-12 05:25
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?
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2006-05-12 07:55
(Received via mailing list)
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.
F5c01ba0de38c0566ced81a688a731b6?d=identicon&s=25 Bonicon (Guest)
on 2006-05-12 09:58
Thanks, I upgraded to 1.1 and it is working as it should now.
This topic is locked and can not be replied to.