I’m having a difficult time grasping polymorphic associations in Rails. I have two models, Group and User.
A User needs to belong to a group, but a Group can have_many users AND have_many groups. I need my groups to be like a tree, which I think the Ancestry gem should help, but I haven’t tried yet.
Seems like I would need some kind of join model, Membership, that has a user_id and a group_id. Then I could do a has_many :through to relate users to groups, but how would I get it to have many groups as well? Would the Membership be the polymorphic model?
Thanks!
Andy
This is not a polymorphic association. A polymorphic association is an association that transcends class types, such as a image class belonging to people and dogs class.
You are talking about Single Table Inheritance where Groups can belong to another group and have other groups. Something like what is below is what you are looking for.
This is just air code, might need some tweaks