Case-insensitive find_or_create_by_whatever
Posted
by Horace Loeb
on Stack Overflow
See other posts from Stack Overflow
or by Horace Loeb
Published on 2010-03-12T20:34:34Z
Indexed on
2010/03/12
20:37 UTC
Read the original article
Hit count: 371
I want to be able to do Artist.case_insensitive_find_or_create_by_name(artist_name)
[1] (and have it work on both sqlite and postgreSQL)
What's the best way to accomplish this? Right now I'm just adding a method directly to the Artist
class (kind of ugly, especially if I want this functionality in another class, but whatever):
def self.case_insensitive_find_or_create_by_name(name)
first(:conditions => ['UPPER(name) = UPPER(?)', name]) || create(:name => name)
end
[1]: Well, ideally it would be Artist.find_or_create_by_name(artist_name, :case_sensitive => false)
, but this seems much harder to implement
© Stack Overflow or respective owner