Roles that are User<>Project Based

Continuing along this thread… I’m interested in creating a SCOPE in
the project model (project.rb) that returns all the project’s members
(bassed on the permissions table <> users & role tables.

Desired output:
user.name, role.name

Here’s my scope in the model, which isn’t returning the desired
output:

class Project < ActiveRecord::Base
has_many :permissions
has_many :users, :through => :permissions
#Playing with Scopes

scope :teammembers,
Project.permissions.joins(:users, :roles)

end

trying to figure out how to get the output with ActiveRecord. Thanks

nobosh wrote:

Continuing along this thread… I’m interested in creating a SCOPE in
the project model (project.rb) that returns all the project’s members
(bassed on the permissions table <> users & role tables.

Desired output:
user.name, role.name

Here’s my scope in the model, which isn’t returning the desired
output:

class Project < ActiveRecord::Base
has_many :permissions
has_many :users, :through => :permissions
#Playing with Scopes

scope :teammembers,
Project.permissions.joins(:users, :roles)

end

trying to figure out how to get the output with ActiveRecord. Thanks

You’re making the exact same mistake as before. Project.permissions
just returns an array of Permission objects.

Best,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Sent from my iPhone

Thanks Marnen, obviously it hasn’t clicked yet but I’ll get it! Am I
at least right trying to get this to work as a Scope in the Project’s
model, so I can have the controller pass the team members array to the
Controller to the view projects> show.html.erb?

Thanks

Alright Marnen, I think I’m getting it… I was able to get the team
members by using the following in the projects controller:

@teammembers = @project.users

That gives me the users, but it doesn’t provide the user’s roles. I
tried including/joining roles to the code above but that errored. Any
tips?

All,

I got this working, but would love to know if this is the way to do it
RIGHT with Rails 3?

@teammembers = @project.users.joins(:permissions
=> :role).select(“roles.name”)

thank you!