2012-06-05 4 views
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 속성을 포함하는 레일의 방법은 무엇인가가 있다면?

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

고마워요!

업데이트 : 아직 문제가 있습니다. 내 목표는 특성에 포함 된 역할을 가진 사용자 모델 배열을 얻는 것입니다.

+0

내가 있었다 일을 결국 무엇'@users = @ item.users.all (: 조인 => : item_user_relationships는, ". 사용자 *, item_user_relationships.role"=> 선택)' – paulnsorensen

답변

0

나는 당신을 정확하게 이해하지만, 아마도 이것이 당신이 원하는 것일 수 있습니까?

@users = @item.users 
@users.each do |user| 
    puts user.item_user_relationships.first.role 
end 
관련 문제