Skip to content

Instantly share code, notes, and snippets.

@pedromg
Created July 18, 2011 17:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pedromg/1090142 to your computer and use it in GitHub Desktop.
Save pedromg/1090142 to your computer and use it in GitHub Desktop.
DataMapper update_or_create
module DataMapper
module Model
# update_or_create method: finds and updates, or creates;
# -upon create, returns the object
# -upon update, returns the object (by default, returned True)
# @param[Hash] Conditions hash for the search query.
# @param[Hash] Attributes hash with the property value for the update or creation of a new row.
# @param[Boolean] Merger is a boolean that determines if the conditions are merged with the attributes upon create.
# If true, merges conditions to attributes and passes the merge to the create method;
# If false, only attributes are passed into the create method
# @return[Object] DataMapper object
def update_or_create(conditions = {}, attributes = {}, merger = true)
begin
if (row = first(conditions))
row.update(attributes)
row
else
create(merger ? (conditions.merge(attributes)) : attributes )
end
rescue
false
end
end
end # Module Model
end # Module DataMapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment