0

I have the following relationships:

# Project
has_many :teammates
has_many :users, through: :teammates

# User
has_many :teammates
has_many :projects, through: :teammates

# Teammate
belongs_to :user
belongs_to :project

The teammate table has a "role" field telling what role the user has in the project. This is how I save a teammate in the DB with the role:

project.teammates.create(:user => current_user, :role => 10)

So this is working. Now what I'd like to do is fetch all users from a particular project, with all users having a role field, telling what role they have in that project.

Would that be possible? I read some posts talking about adding an attribute_accessible to the user model, but that's not in rails 4 anymore.

gkpo
  • 2,623
  • 2
  • 28
  • 47

1 Answers1

0

Alright, so I found the solution here:

This works

has_many :users, -> { select('users.*, teammates.role as teammate_role') }, through: :teammates
Community
  • 1
  • 1
gkpo
  • 2,623
  • 2
  • 28
  • 47