2014-04-16 3 views
0

예약 - < 주문 - < 거래 레일 4.1 중첩 된 모델 양식 필드

class Booking < ActiveRecord::Base 
    has_many :orders 
end 

class Order < ActiveRecord::Base 
    belongs_to :booking 
    has_many :transactions 
end 

class Transaction < ActiveRecord::Base 
    belongs_to :order 
end 

가 나는 Order 또는 Booking 기존없이 Transaction을 만들 수 있어야합니다.

나는 다음과 같은 달성하기 위해 노력하고있어 : Transaction가 생성 될 때 OrderBooking가 자동으로 생성됩니다. 거래 양식은 Booking.booking_number을 취할 수 있으며 위의 내용은 자동으로 Booking으로 생성됩니다.

나는 레일에 매우 새롭고 accepts_nested_attributes_for, Ryan Bates의 nested model form part1 screencastform_fields_for의 조합을 성공하지 못했습니다.

일부 지침은 반드시 코드 일 필요는 없지만 대단히 감사하겠습니다. 이 그것이 될 수 있습니다 작업을 시도 enter image description here

답변

1

에 따라 여부를 모델에서 는 accepts_nested_attributes_for :order, :allow_destroy => true 변화.

잘못된 시스템 설계 - 반드시 거래 을 따르시겠습니까?

귀하의 질문에 우선 예약 또는 주문을 만드는 것이 좋습니다. 주문 후

당신에게 할당 트랜잭션 &를 만드는 당신에게 을 중지 아무것도, 그럼에도 불구하고

#app/controllers/bookings_controller.rb 
Class BookingsController < ApplicationController 
    def create 
     booking = Booking.new(booking_params) 
     booking.save 
    end 
end 

#app/models/booking.rb 
Class Booking < ActiveRecord::Base 
    before_create :build_transaction #-> creates a blank transaction which can be populated later 
end 

: 이것은 당신이 order 또는 booking에 볼트 체결로 거래를 만들 수 있습니다 이것을 할 수 있습니다 :

#app/controllers/transactions_controller.rb 
def create 
    Transaction.new(transaction_params) 
end 

#app/models/transaction.rb 
Class Transaction < ActiveRecord::Base 
    after_create :order 

    def order 
     self.order.create!([order_details?]) 
    end 
end 

건물에 대한 정보를 좀 더 알려 주면보다 정교한 응답을 작성할 수 있습니다.

0

:

내 경로는 다음과 같다. 참/거짓이 폼 나는 기존 주문 또는 예약 없이 트랜잭션을 생성 할 수 있어야합니다

+0

감사합니다.하지만 그 질문에는 아무런 관련이 없습니다. – EasyCo