如何在不再次调用 .save 的情况下将子记录的 ID 保存在父记录中



我有一个问题,在 Rails 5.1 之前,遗留代码在 after_save 回调中调用.save,但由于saved_change_to_attribute如何在after_回调中替换attribute_changed?,因此调用.save两次是一个问题(.save会影响.changes(。

我现在需要复制相同的行为,而无需调用.save两次。

在我的 Rails 5.1.7 应用程序中(准备 5.2 升级(:

class OtherModel < ApplicationRecord
  belongs_to :my_model, inverse_of: :other_model_history
end
class MyModel < ApplicationRecord
  has_many :other_model_history, class_name: 'OtherModel', inverse_of: :my_model
  belongs_to :latest_other_model, class_name: 'OtherModel'
  before_create :initialize_first_other_model
  def initialize_first_other_model
    initial_other = OtherModel.new(name: 'initial')
    other_model_history << initial_other
    latest_other_model = initial_other
    # When it finally reaches save (this is before_create) I want ActiveRecord to know
    # to save the ID of initial_other in latest_other_model_id ... but it doesn't
  end
end

调用 MyModel.new.save 时,initialize_other_model 会根据 my_model.id 创建具有正确other_model.my_model_id的初始OtherModel实例。

但是,my_model.latest_other_model_id为零。尽管 latest_other_model正确引用initial_other对象。

我怎样才能告诉活动记录它必须设置my_model.latest_other_model_id = initial_other.id

编辑:与ActiveRecord::Base.logger = Logger.new(STDOUT)

DEBUG -- :   SQL (0.3ms)  INSERT INTO "my_models" ("created_at", "updated_at") VALUES (?, ?)  [["created_at", "2019-05-31 06:12:27.006455"], ["updated_at", "2019-05-31 06:12:27.006455"]]
DEBUG -- :   SQL (0.1ms)  INSERT INTO "other_models" ("my_model_id", "name", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["my_model_id", 3], ["name", "initial"], ["created_at", "2019-05-31 06:12:27.015472"], ["updated_at", "2019-05-31 06:12:27.015472"]]
DEBUG -- :    (1.5ms)  commit transaction

您可以看到首先插入MyModel,没有other_model,然后插入OtherModel带有MyModel中的id。我希望 Rails 也知道然后插入my_models.latest_other_model_id,而不调用.save

我能够在after_save中使用update_column。这会将引用从my_model保存到other_model,而不会影响saved_changes。我的回调现在是

before_create:

  initial_other = OtherModel.new(name: 'initial')
  other_model_history << initial_other

after_save:

update_column('latest_other_model_id', other_model_history.last.id)

注意:我不建议任何没有被遗留代码强加给他们的人使用这种在回调内初始化的风格。这感觉非常脆弱。

最新更新