Skip to content

Instantly share code, notes, and snippets.

@pedromg
Created October 12, 2010 02:20
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/621551 to your computer and use it in GitHub Desktop.
Save pedromg/621551 to your computer and use it in GitHub Desktop.
module DataMapper
module Model
# update_or_create method: finds and updates, or creates;
# merger is a boolean that determines if the conditions are merged with the attributes upon create;
# merge = true => merges conditions to attributes and passes the merge to the create method
# merge = false => only attributes are passed into the create method
def update_or_create(conditions = {}, attributes = {}, merger = true)
(first(conditions) && first(conditions).update(attributes)) || create(merger ? (conditions.merge(attributes)) : attributes )
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