保存accepts_nested_attributes_for的顺序

时间:2015-11-03 17:33:25

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

我最近将我的应用从v4.2.0升级到v4.2.4。事实证明,嵌套属性的保存订单已更改。有没有办法来定义哪些&accept; accepted_nested_attributes_for'先保存我能够注意到这种变化,因为每个模型都有before_create回调。

更新

当我们从v4.2.0切换到v4.2.1时,问题就开始了。

更新2:为什么订单很重要?

因为我们有customer的单一注册表单,accepts_nested_attributes_for creditcardsubscriptioncreditcardsubscription回调的顺序很重要,因为一旦信用卡'调用before_create回调,我们可以在条带上远程创建订阅。

更新3:

class Customer < ActiveRecored::Base
    has_one :subscription
    has_many :creditcards

    accepts_nested_attributes_for :creditcards
    accepts_nested_attributes_for :subscription
end

class Creditcard < ActiveRecord::Base
    belongs_to :customer

    # needs to run before Subscription before_create callback
    before_create :create_stripe_creditcard
end

class Subscription < ActiveRecord::Base
    belongs_to :customer

    before_create :create_stripe_subscription
end

1 个答案:

答案 0 :(得分:1)

要在Creditcard之前保存Subscription,您只需要更改关联顺序&#39;声明。

所以Customer模型应如下所示:

class Customer < ActiveRecored::Base
    has_many :creditcards
    has_one :subscription

    ...
end