2010-01-28 4 views
0

내가 4 개 모델을 가지고 : TRANSAC는, transac_data이 항목은 "TRANSAC"보기 지금 레일 has_many 관계 (4 개 모델) 및

class Transac < ActiveRecord::Base 
    has_many :transac_datas 
    has_many :items, :through => :transaction_datas 
end 

class TransactionData < ActiveRecord::Base 
    belongs_to :item 
    belongs_to :transaction 
end 

class Item < ActiveRecord::Base 
    has_many :transaction_datas 
    has_many :transacs, :through => :transaction_datas 
end 

class DvdDetails < ActiveRecord::Base 
    has_many :items 
end 

을 dvd_details, 나는에서 물건에 접근 할 필요가 모든 모델 같은 :

<td><%=h transac.status %></td> 
<% transac.transaction_datas.each do |td| %> 
    <td><%=h td.item_type %></td> 
<% end %> 

<% transac.items.each do |item| %> 
    <td><%=h item.item_type %></td> 
<% end %> 

하지만 나는 또한 "멀리"떨어져 TRANSAC에서입니다 "DvdDetails"모델에서 약간의 정보에 액세스해야합니다.

나는이 같은 일을하는 것은 정말 작동하지 않을 것을 깨달았다 :

class Transac < ActiveRecord::Base 
    has_many :transac_datas 
    has_many :items, :through => :transaction_datas 
    has_many :dvd_details, :through => :items, :through => :transaction_datas 
end 

을하고 "TRANSAC"뷰의 인덱스에서이 작업을 수행 내가 달성하기 위해 어떻게해야합니까 무엇

<%=h transac.dvd_details.name %> 

이?

도움을 주시면 감사하겠습니다. 감사합니다.

답변

0

실제로는 Ian White's nested_has_many_through plugin으로 원하는 방식대로 데이지 체인으로 연결할 수 있습니다. 그냥과 같이 플러그인을 설치 : 모델이 같은

script/plugin install git://github.com/ianwhite/nested_has_many_through.git 

그런 설정 :

class Transac < ActiveRecord::Base 
    has_many :transaction_datas 
    has_many :items, :through => :transaction_datas 
    has_many :dvd_details, :through => :items 
end 

이것은 당신이 필요로해야한다.

업데이트 :이 질문은 최근에 몇 차례 나 나왔습니다. 나는 상세한 설명을 위해 nesting your has_many :through relationships이라는 기사를 썼다. GitHub에 첨부 된 예제 응용 프로그램도 다운로드하여 사용할 수 있습니다.

+0

PS - 중첩 has_many : 연관성을 통해 맥주처럼 - 적당히 즐기십시오 :) –

+0

하하, 멋지다! 도와 줘서 고맙다! –

+1

흥미롭게도, 누군가가 내 대답을 싫증 났지만 완벽하게 작동한다고해도 스스로 해답을 제시하지 못했습니다. –