Skip to content

Instantly share code, notes, and snippets.

@rsl
Created March 4, 2009 20:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rsl/73998 to your computer and use it in GitHub Desktop.
Save rsl/73998 to your computer and use it in GitHub Desktop.
class User < ActiveRecord::Base
def merge(other)
%w{
attribute_1
attribute_2
attribute_3
attribute_4
attribute_5
attribute_6
}.each do |attribute|
self.write_attribute(attribute, oldest(attribute, other))
end
%w{
attribute_7
attribute_8
attribute_9
}.each do |attribute|
self.write_attribute(attribute, first_non_blank(attribute, other))
end
# Unrelated code here ...
end
private
def oldest(attribute, other)
oldest ||= created_at < other.created_at ? self : other
oldest.send(attribute)
end
def first_non_blank(attribute, other)
value = self.send(attribute)
value.blank? ? other.send(attribute) : value
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment