Reputation: 2363
I have a project management app that has 3 models…Project, Users, and Participation. I would like to be able to show a list of all the Users who have collaborated with a given User on any of the Projects the Given User was a Participant in. This includes past Projects.
For Instance UserA & UserB were in ProjectBlue UserA & UserC were in ProjectYellow UserA & UserC & UserD were in ProjectRed
If I am in UserA’s dashboard I want to be able to see a list that says:
"You’ve worked with: UserC(only listed once) UserD UserB”
Do I need to create a whole :relationships Table with a :user_id, :project_id, record for EVERY shared connection? This doesn’t seem efficient.
Models
Class Project < ActiveRecord::Base
belongs_to :user # (this is the organizer)
has_many :users, through: :participants
has_many :participants
Class User < ActiveRecord::Base
has_many :projects
has_many :project, through :participant
has_many :participants
Class Participant < ActiveRecord::Base
belongs_to :project
belongs_to :user
Upvotes: 1
Views: 61
Reputation: 8318
That is the correct way. Database space is free and the database is bored anyway. It'll appreciate some data to work with. ;-)
Have a look at http://www.xyzpub.com/en/ruby-on-rails/4.0/ar-many_to_many.html for some examples.
But I'm not sure about your models. They do seem to be a bit strange. You probably want:
Class Project < ActiveRecord::Base
belongs_to :user # (this is the organizer)
has_many :participants
has_many :users, through: :participants
Class User < ActiveRecord::Base
has_many :participants
has_many :projects, through: :participants
Class Participant < ActiveRecord::Base
belongs_to :project
belongs_to :user
Upvotes: 1