Skip to content

Instantly share code, notes, and snippets.

@glongman
Created August 4, 2009 03:24
Show Gist options
  • Save glongman/161003 to your computer and use it in GitHub Desktop.
Save glongman/161003 to your computer and use it in GitHub Desktop.
class MyModel < ActiveRecord::Base; end
# example model has 3 fields guid, name, and description
# find_or_create_by_name
mymodel = MyModel.find_or_create_by_name(params[:name) do |created|
created.guid = GuidGen.generate
created.description = params[:description]
end
# find_or_intialize_by_name
mymodel = MyModel.find_or_initialize_by_name(params[:name) do |inited|
inited.guid = GuidGen.generate
inited.description = params[:description]
end
mymodel.save if mymodel.new_record?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment