0

所以如果我有以下关系

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

role列出项目的所有用户时包含该属性的 Rails 方式是什么?

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

谢谢!

更新:我仍然有这个问题。我的目标是获得一组用户模型,它们的角色包含在属性中。

4

1 回答 1

0

我确定我是否理解正确,但也许这就是你想要的?

@users = @item.users
@users.each do |user|
  puts user.item_user_relationships.first.role
end
于 2012-06-05T15:28:52.657 回答