我最近将我的应用从v4.2.0升级到v4.2.4。事实证明,嵌套属性的保存订单已更改。有没有办法来定义哪些&accept; accepted_nested_attributes_for'先保存我能够注意到这种变化,因为每个模型都有before_create回调。
当我们从v4.2.0切换到v4.2.1时,问题就开始了。
因为我们有customer
的单一注册表单,accepts_nested_attributes_for
creditcard
和subscription
。 creditcard
和subscription
回调的顺序很重要,因为一旦信用卡'调用before_create
回调,我们可以在条带上远程创建订阅。
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
答案 0 :(得分:1)
要在Creditcard
之前保存Subscription
,您只需要更改关联顺序&#39;声明。
所以Customer
模型应如下所示:
class Customer < ActiveRecored::Base
has_many :creditcards
has_one :subscription
...
end