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!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs