0

So if I have the following relationship

class Item < ActiveRecord::Base
   has_many :item_user_relationships
   has_many :users, :through => :item_user_relationships
end

class User < ActiveRecord::Base
   has_many :item_user_relationships
   has_many :items, :through => :item_user_relationships
end

class ItemUserRelationship < ActiveRecord::Base
   belongs_to :item
   belongs_to :user

   attr_accessible :role
end

What's the rails way to include the role attribute when listing all the Users of an Item?

@users = @item.users # I want to include the role as part of a user 

Thanks!

UPDATE: I'm still having trouble with this. My goal is to get an array of User models that have their role included in the attributes.

paulnsorensen
  • 448
  • 4
  • 10
  • What I ended up doing was `@users = @item.users.all(:joins => :item_user_relationships, :select => "users.*, item_user_relationships.role")` – paulnsorensen Jun 05 '12 at 07:32

1 Answers1

0

I'm note sure if I understand you correctly, but maybe this is what you want?

@users = @item.users
@users.each do |user|
  puts user.item_user_relationships.first.role
end
trautwein
  • 480
  • 1
  • 5
  • 14