2014-05-23 3 views
1

has_many Task_Orders 계약 모델이 있습니다. Contract 광고 항목에 대해 '표시'를 클릭하면 해당 계약에 속한 Task_Orders 목록이 표시되는보기를 렌더링하려고합니다.부모 레일의 자식 렌더링 4.0

다음
class Contract < ActiveRecord::Base 

has_many :task_orders 

end 

내 작업 주문 모델 : 여기

create_table "task_orders", force: true do |t| 
t.integer "contract_Id",    limit: 255 
t.string "task_orderId" 
t.string "task_orderName" 

내 계약 모델 :

여기
create_table "contracts", force: true do |t| 
t.string "contractId" 
t.string "contractName" 

내 작업 주문 스키마입니다 : 여기

내 계약 스키마입니다 :

class TaskOrder < ActiveRecord::Base 

belongs_to :contract 

end 

컨트롤러와 작업하는 방법을 잘 모르겠습니다 .... 도와주세요. 레일즈 4.0을 사용 중입니다.

고맙습니다.

+1

가능한 중복

#app/controllers/contracts_controller.rb Class ContractsController < ApplicationController def show @contract = Contract.find params[:id] end end #app/views/contracts/show.html.erb <% for order in @contract.task_orders do %> <%= order.id %> <% end %> 
[부모 클래스의 표시 아이들이 레일 4.0 (http://stackoverflow.com/questions/23833091/show-children-of-a-parent-class-rails-4-0) –

답변

1

당신의 foreign_keys이 당신의 연관들에 대해 할당하기 위해

은 첫째, 당신이 필요로 foreign_key :

#app/models/task_order.rb 
Class TaskOrder < ActiveRecord::Base 
    belongs_to :contract, primary_key: "contractID", foreign_key: "contract_Id" 
end 

#app/models/contract.rb 
Class Contract < ActiveRecord::Base 
    has_many :task_orders, primary_key: "contractID", foreign_key: "contract_Id" 
end 

-

컨트롤러에게

을이 당신을 허용해야 귀하의 컨트롤러에서 필요한 데이터를 호출 :