0

My application manages business rules, and nested tasks. During a normal creation process, some attributes are initialised in a call-back, such as before_create :set_hierarchy, for each object (rule & tasks).

For managing versions of a business rule, I dup it, including nested tasks. In this case, the before_create call-back must not be executed neither for the business rule, nor for the tasks.

Disabling a call-back off BusinessRule class is a known operation, but how to disable the call-back off the nested class?

My code is:

BusinessRule.without_callback(:create, :before, :set_hierarchy) do
  @business_rule_version = @business_rule.dup 
  @business_rule.tasks.each do |task| # Duplicate associated tasks too
    dup_task = task.dup
    dup_task.business_rule_id = @business_rule_version.id
    dup_task.save
  end
end

This works fine for the business rule, but how can I specify this without_callback clause for the nested tasks? I tried the deep_clone gem, which leaves me with the same question ...

Thanks a lot for your help!

user1185081
  • 1,898
  • 2
  • 21
  • 46
  • 1
    Possible duplicate of [How can I avoid running ActiveRecord callbacks?](https://stackoverflow.com/questions/632742/how-can-i-avoid-running-activerecord-callbacks) – pdu Oct 31 '19 at 14:47
  • 1
    Instead of setting callbacks on these objects and then trying to avoid them, would it be possible to add some conditionals to your callbacks so they're only executed when needed? Could you show an example callback? – calebkm Oct 31 '19 at 18:24

0 Answers0