如果参数丢失,请保存关联的一部分

时间:2016-02-09 13:40:15

标签: ruby-on-rails ruby-on-rails-4

该关联创建了customer_order和payment。如果缺少payment_amt,我希望能够单独保存customer_order。

如果我在没有payment_amt的情况下尝试保存,则保存失败,因为它试图保存到两个模型。

{"utf8"=>"✓", "authenticity_token"=>"eTjUanQFPW1haHqfIALNLvDfHEe4TMxYo1OL8XpbZXX9MOiGcqpLrd9rVKksnxblzu2IKzwFx7pwTvDFWAKcNA==", "customer_order"=>{"dba_id"=>"20", "order_amt"=>"100", "payments_attributes"=>{"0"=>{"payment_amt"=>"", "transaction_id"=>"1234", "transaction_type"=>"CHK"}}, "magazine_id"=>"339", "years"=>"2", "lead_id"=>"594", "mailer_id"=>"", "source"=>"custom entry", "new_or_renewal"=>"N"}, "commit"=>"Create Customer order", "controller"=>"customer_orders", "action"=>"create"}

如下所示,付款缺失。在这种情况下,我只想保存客户订单,这将在稍后的情况下创建账单。

@customer_order.payments
#<ActiveRecord::Associations::CollectionProxy [#<Payment id: nil,  payment_amt: nil, transaction_type: "CHK", transaction_id: "1234",   deposit_id: nil, user_id: nil, created_at: nil, updated_at: nil, customer_order_id: nil, deposit_created_at: nil>]>

2 个答案:

答案 0 :(得分:0)

我想您的CustomerOrder模型中有accepts_nested_attributes_for :payments, reject_if: proc { |payment| attributes['payment_amt'].blank? } 。将其更改为:

@Component(
    selector: 'test_component',
    templateUrl: 'test_component.html',
    useShadowDom: false
)
class TestComponent {

    bool clicked = false;
    clickHandler() {
        clicked = true;
    }
}

答案 1 :(得分:0)

如果缺少付款信息,我最终删除了该关联

@customer_order.payments.delete_all

然后我单独保存customer_order。