Skip to content

Instantly share code, notes, and snippets.

@vedant1811
Last active August 29, 2015 14:17
Show Gist options
  • Save vedant1811/e942f03cd8e2168b74d2 to your computer and use it in GitHub Desktop.
Save vedant1811/e942f03cd8e2168b74d2 to your computer and use it in GitHub Desktop.
find_or_create_by with the option to pass (..., case_sensitive: false)
# from http://stackoverflow.com/a/2439293/1396264
def self.find_or_create_by_name(*args)
puts args.inspect
options = args.extract_options!
options[:name] = args[0] if args[0].is_a?(String)
case_sensitive = options.delete(:case_sensitive)
conditions = case_sensitive ? ['name = ?', options[:name]] :
['UPPER(name) = ?', options[:name].upcase]
where(conditions).first || create(options)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment