In Rails, how can a record belong_to a single user, but also have multiple "secondary" users?

Posted by Kuro on Stack Overflow See other posts from Stack Overflow or by Kuro
Published on 2010-04-14T22:17:28Z Indexed on 2010/04/15 0:53 UTC
Read the original article Hit count: 314

Filed under:
|
|

In my app, I have a User model and a Project model. A user has_many assignments and each project belongs_to a user. But along with each project having an owner, the user who created it, I would like the owner be able to share it with others (so that the project gets shown on the other users' account along with their own). I imagine having to use has_many :through, and setting up a projects_users table with a user_id and a project_id. And I guess this would be the end result?

Project.first.user
  # The creator of the project
  => #<User id: 1, name: 'andrew', etc...>

Project.first.users
  # The users that the creator chose to share it with
  => [#<User id: 2 ...>, #<User id: 3 ...>]

How would I go about doing this? Thanks!

© Stack Overflow or respective owner

Related posts about rails

Related posts about model