Sunday, 15 September 2013

rails - single table inheritance and has_and_belongs_to_many (both inheriting tables with many to many relations with another model)

rails - single table inheritance and has_and_belongs_to_many (both
inheriting tables with many to many relations with another model)

class User ..
class UserA < User
class UserB < User
class Biscuit < Active...
Now, lets say, UserA has many biscuits and a biscuit has many UserA's.
Then we can do like:
class UserA < User
has_and_belongs_to_many :biscuits
end
class Biscuit < User
has_and_belongs_to_many :useras
end
Migration:
create_table :biscuits_users do |t|
#notice name: _users and not _useras. Biscuit instance expecting user
table name after underscore so may be if we can make biscuit expect
separately like biscuit_useras and biscuit_userbs. But not sure how to do.
t.belongs_to :usera
t.belongs_to :biscuit
end
Requirement:
What if both UserA and UserB has many to many relation with Biscuit?
Since, UserA and UserB has a common table, User, the migration table will
be biscuits_users whether we want it to be UserA or UserB.
Not just Biscuit that is having many to many with them. Many such models
have many to many relation with both UserA and UserB. So, how ?
Please let me know for more info.
Thanks.

No comments:

Post a Comment