Last active
August 29, 2015 14:17
-
-
Save vedant1811/e942f03cd8e2168b74d2 to your computer and use it in GitHub Desktop.
find_or_create_by with the option to pass (..., case_sensitive: false)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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