Case-insensitive find_or_create_by_whatever

后端 未结 4 1067
天命终不由人
天命终不由人 2021-01-12 15:58

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

4条回答
  •  耶瑟儿~
    2021-01-12 16:32

    Rails 4 gives you a way to accomplish the same thing:

    Artist.where('lower(name) = ?', name.downcase).first_or_create(:name=>name)

提交回复
热议问题