A user has_many projects, a Project has_many Users, but each role is
associated with 1 user + 1 Project ... i suggest to use has_many
:through and use a join table "project_meberships"
class User
has_many :project_memberships
has_many :projects, :through => :project_memberships
end
class Project
has_many :project_memberships
has_many :users, :through => :project_memberships
end
class ProjectMembership
belongs_to :user
belongs_to :project
belongs_to :role
end
class Role
has_many :project_memberships
end
the table "project_memberships" should look something like this:
id #primary key, auto increment
user_id
project_id
role_id
(additional fields like description)
This assumes you have pre-defined Roles like Admin, Teamleader,
Participant etc. pp. in the roles table.
@user = User.find(1)
role = @user.project_memberships(1).role
project = @user.project_memberships(1).project
You might want to read this about eager loading those associations with
a nested :include in the find command.
http://www.bigbold.com/snippets/posts/show/2089
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---