moiristo / deep_cloneable

This gem gives every ActiveRecord::Base object the possibility to do a deep clone that includes user specified associations.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Any way to skip after_create callback?

flamontagne opened this issue · comments

Is there a way to prevent after_create callbacks from being called on the main record plus all associations when calling deep_clone? I have an after_create method for populating default data, but I don't want it to execute when cloning the record.

Thanks for this great gem.

Unfortunately not, it isn't rally a concern for this gem. Personally, I'd create an accessor, set that accessor using the optional block of the deep_clone method and check the value before executing the callback. Simple (incomplete) example:

class Node < ActiveRecord::Base
  has_many :nodes
  attr_accessor :skip_callback
  after_create :populate, unless: :skip_callback
end

deep_copy = Node.find(1).deep_clone(include: :nodes) do |original, kopy|
  # This block will be exeuted for every copied record!
  kopy.skip_callback = true
end

deep_copy.save!

That's perfect! I didn't know we could use the block to skip the callback. Thanks a lot!

Glad I could help!