module ARMerge::InstanceMethods
Public Instance Methods
merge!(other, options={})
click to toggle source
# File lib/ar_merge.rb, line 6 def merge!(other, options={}) raise "cannot merge wit a new record" if other.new_record? raise "cannot merge with myself" if other == self #merge associations (options[:associations]||[]).each do |association_name| send(association_name).concat other.send(association_name) if ActiveRecord::VERSION::MAJOR < 4 #update counters, this is very basic/hacky/not secure for customized counters... counter = "#{association_name}_count" next unless other.respond_to?(counter) and respond_to?("#{counter}=") self.class.update_counters(id, counter => other.send(counter)) end end #merge attributes (options[:attributes]||[]).each do |attr| send("#{attr}=", other.send(attr)) if send(attr).blank? end #cleanup other.reload.destroy save! end