在rails 5中构建has_many关系对象时如何跳过callback_callback

问题描述 投票:0回答:1

在rails 5中构建has_many关系对象时如何跳过_回调

考虑以下情况

class Customer
  has_many :resources
end

class Resource
  attr_accessor: :skip_callback
  belongs_to :customer
  after_commit :data_calculation, unless: :skip_callback

  def data_calculation
    # logic goes here
  end
end

customer = Customer.new
customer.resources.build({name: 'abc'})
customer.save

我要跳过关联对象的回调。

我们在构建对象时可以这样做吗?

ruby-on-rails ruby-on-rails-5 rails-activerecord model-associations
1个回答
0
投票

找到解决方案,

customer = Customer.new
customer.resources.build({name: 'abc', skip_callback: true})
customer.save

将attr_accessor作为参数传递也会设置回调条件值。

© www.soinside.com 2019 - 2024. All rights reserved.