0

I have a problem with habtm on a single model in rails.

Example: Let us say i have a User model with two roles "Student" and "teacher". User model is common for two roles. Now

Each student can be associated to many teachers

Each teacher can be associated to many students

In rails notation, their should be habtm between teacher and student How this can be achieved with single table.

Thanks, Aashish

aashish
  • 2,453
  • 1
  • 21
  • 19

1 Answers1

1

It can't be done with a single table. In a many-to-many relationship, no matter what, you always need a table where you store the associations.

In your case, given the association seems to be parent/child, then you just need two tables instead of one.

How to implement it, it depends on your database structure and data organization. You should create an users_users table (as part of the habtm) and configure the references accordingly. If the user table, as it seems to be, is also used for STI, then the configuration may change a little bit.

Simone Carletti
  • 173,507
  • 49
  • 363
  • 364