Leevi Graham
2006-Aug-16 09:41 UTC
[Rails] Polymorphic Associations and grouping users and projects
Hi all, In my app I would like to create user groups in my admin. Currently I have the following models and associations that facilitate this. class User < ActiveRecord::Base has_many :memberships has_many :groups, :through => :memberships end class Membership < ActiveRecord::Base belongs_to :group belongs_to :user end class Group < ActiveRecord::Base has_many :memberships has_many :users, :through => :memberships end What I would like to do is add project groupings to the admin and I think I may need to use polymorpic associations. However I dont know if this is he best option. For someone who is much brighter than me, is the code below the best way to achieve this? class User < ActiveRecord::Base has_many :memberships, :as => :groupabale has_many :groups, :through => :memberships end class Project < ActiveRecord::Base has_many :memberships, :as => :groupable has_many :groups, :through => :memberships end class Membership < ActiveRecord::Base belongs_to :group belongs_to :groupable, :poymorphic => true end class Group < ActiveRecord::Base has_many :memberships end Thanks for your input! -- Posted via http://www.ruby-forum.com/.
Apparently Analagous Threads
- Polymorphic Association with Single Table Inheritance?
- Naming rights_roles join model using has_many :through and polymorphic associations
- using polymorphic associations with acts_as_list
- Polymorphic, many-to-many, self-referential data model
- RESTful controller naming conventions