gpt4 book ai didi

mysql - 保存 accepts_nested_attributes_for 的顺序

转载 作者:行者123 更新时间:2023-11-28 23:46:14 29 4
gpt4 key购买 nike

我最近将我的应用程序从 v4.2.0 升级到了 v4.2.4。事实证明,嵌套属性的保存顺序已更改。有没有办法定义先保存哪个“accepts_nested_attributes_for”。我能够注意到变化,因为每个模型都有 before_create 回调。

更新:

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

更新 2:为什么顺序很重要?

因为我们有一个针对客户的注册表单,它accepts_nested_attributes_for creditcardsubscriptioncreditcardsubscription 回调的顺序很重要,因为一旦信用卡的 before_create 回调被调用,我们就可以在 stripe 上远程创建订阅。

更新 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

最佳答案

为了在订阅之前保存Creditcard,您只需更改协会声明的顺序。

所以 Customer 模型应该是这样的:

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

...
end

关于mysql - 保存 accepts_nested_attributes_for 的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33505577/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com