ruby on rails - How to make find_or_initialize case insensitive on a Postgres DB? -


in rails 3.2 app have post model belongs category. in new post form text field assigning category or creating new category.

it works via following method on post model

def category_name=(name)   if name.present?     post_cat = category.find_or_initialize_by_name(name)      if post_cat.new_record?       post_cat.save(:validate => false)       self.category = post_cat     else       self.category = post_cat     end   end end 

this more or less working, case sensitive. example, database contains records "featured", "featured" , "featured" categories.

how can make above find_or_initialize function behave though case insensitive. using postgres database, , have suspicion not quite easy "making find_or_initialize case insensitive".

i'm grateful advice best practice, consider, , useful references.

i refer comment above: maybe add second field, containing value of string displayed user, , find_or_initialize name column:

post_cat = category.find_or_initialize_by_name(name.downcase) post_cat.display_name = name if post_cat.new_record? 

so second line ensures, existing record doesn't overriden user, tries add categories brandname. pitfall see is, first user, creates category, has spell correct.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -