Ruby on rails Rails-检查父对象是否已销毁并且正在对依赖项调用销毁

Ruby on rails Rails-检查父对象是否已销毁并且正在对依赖项调用销毁,ruby-on-rails,activemodel,Ruby On Rails,Activemodel,我有以下资料: class ModelA < ApplicationRecord has_many :model_bs, dependent: :destroy end class ModelB < ApplicationRecord belongs_to :model_a after_destroy :action_only_if_model_a_exists private def action_only_if_model_a_exists #

我有以下资料:

class ModelA < ApplicationRecord
  has_many :model_bs, dependent: :destroy
end

class ModelB < ApplicationRecord
  belongs_to :model_a
  after_destroy :action_only_if_model_a_exists

  private

  def action_only_if_model_a_exists
    # Do things
  end
end
class ModelA < ApplicationRecord
  attr_accessor :destroying?
  has_many :model_bs, dependent: :destroy

  def destroy
    self.destroying? = true
    super
  end
end

class ModelB < ApplicationRecord
  belongs_to :model_a
  after_destroy :action_only_if_model_a_exists

  private

  def action_only_if_model_a_exists
    if !model_a.destroying?
      # Do things
    end
  end
end

实现这一点最干净的方法可能是在
ModelA
中使用
before\u destroy
回调,在所有相关的
ModelB
上调用所需的函数

class ModelA < ApplicationRecord
  has_many :model_bs, dependent: :destroy

  before_destroy { |model_a| model_a.model_bs.each { |model_b| model_b. action_only_if_model_a_exists } }
end

实现这一点最干净的方法可能是在
ModelA
中使用
before\u destroy
回调,在所有相关的
ModelB
上调用所需的函数

class ModelA < ApplicationRecord
  has_many :model_bs, dependent: :destroy

  before_destroy { |model_a| model_a.model_bs.each { |model_b| model_b. action_only_if_model_a_exists } }
end

我找不到一个很好的方法来做这件事,所以我决定如下:

class ModelA < ApplicationRecord
  has_many :model_bs, dependent: :destroy
end

class ModelB < ApplicationRecord
  belongs_to :model_a
  after_destroy :action_only_if_model_a_exists

  private

  def action_only_if_model_a_exists
    # Do things
  end
end
class ModelA < ApplicationRecord
  attr_accessor :destroying?
  has_many :model_bs, dependent: :destroy

  def destroy
    self.destroying? = true
    super
  end
end

class ModelB < ApplicationRecord
  belongs_to :model_a
  after_destroy :action_only_if_model_a_exists

  private

  def action_only_if_model_a_exists
    if !model_a.destroying?
      # Do things
    end
  end
end
class ModelA
我找不到一个很好的方法来做这件事,所以我决定做以下几件事:

class ModelA < ApplicationRecord
  has_many :model_bs, dependent: :destroy
end

class ModelB < ApplicationRecord
  belongs_to :model_a
  after_destroy :action_only_if_model_a_exists

  private

  def action_only_if_model_a_exists
    # Do things
  end
end
class ModelA < ApplicationRecord
  attr_accessor :destroying?
  has_many :model_bs, dependent: :destroy

  def destroy
    self.destroying? = true
    super
  end
end

class ModelB < ApplicationRecord
  belongs_to :model_a
  after_destroy :action_only_if_model_a_exists

  private

  def action_only_if_model_a_exists
    if !model_a.destroying?
      # Do things
    end
  end
end
class ModelA
您可以使用子对象中的
被销毁方关联
属性查看对象是否作为父对象的
依赖::销毁
的一部分被销毁。

您可以使用子对象中的
被销毁方关联
属性查看对象是否作为
依赖::从其父级销毁。

您是希望将模型b的ID作为策略清空,还是实际删除记录?而且您是否担心会有几毫秒的延迟,或者即使调用了modela.destroy,您实际上也不希望modela被销毁?我希望删除记录。只有当model\u a\u存在时,
action\u的内容才会启动一个需要几分钟才能完成的异步进程完成,如果相应的ModelA不再存在,则将失败。我感到困惑。如果您调用modela.destroy,那么在modelb被销毁时,modela应该在db进程完成后立即被销毁,因为您调用了一个方法来完成这项工作。似乎最好的方法是从控制器调用modela.modelb.destroy_,然后在确认modelb的过程完成后,在第二次调用中显式销毁modela。操作顺序如下:
model\u a before\u destroy回调
--
model\u b before\u destroy回调
--
model\u b after\u destroy回调
意思是当model\u b被销毁时,模型a仍然存在于数据库中。您是希望作为策略清空模型b的ID,还是实际删除记录?而且您是否担心会有几毫秒的延迟,或者即使调用了modela.destroy,您实际上也不希望modela被销毁?我希望删除记录。只有当model\u a\u存在时,
action\u的内容才会启动一个需要几分钟才能完成的异步进程完成,如果相应的ModelA不再存在,则将失败。我感到困惑。如果您调用modela.destroy,那么在modelb被销毁时,modela应该在db进程完成后立即被销毁,因为您调用了一个方法来完成这项工作。似乎最好的方法是从控制器调用modela.modelb.destroy_,然后在确认modelb的过程完成后,在第二次调用中显式销毁modela。操作顺序如下:
model\u a before\u destroy回调
--
model\u b before\u destroy回调
--
model\u b after\u destroy回调
意思是当model\u b被销毁时,模型a仍然存在于数据库中。我想我的问题一定不清楚-这与我实际追求的行为相反:我只想在模型a存在时触发
模型b.destroy
,如果调用了
model\u a.destroy
,则不会。我想我的问题一定不清楚-这与我实际追求的行为相反:我只希望
action\u仅在存在
model\u a\u时触发
model\u b.destroy
,而不是调用
model\u a.destroy