不区分大小写的 find_or_create_by_whatever
我希望能够执行 Artist.case_insensitive_find_or_create_by_name(artist_name)
[1] (并让它在 sqlite 和 postgreSQL 上工作)
实现此目的的最佳方法是什么?现在我只是直接向 Artist
类添加一个方法(有点难看,特别是如果我想在另一个类中使用此功能,但无论如何):
def self.case_insensitive_find_or_create_by_name(name)
first(:conditions => ['UPPER(name) = UPPER(?)', name]) || create(:name => name)
end
[1]:嗯,理想情况下是Artist.find_or_create_by_name(artist_name, :case_sensitive => false)
,但这似乎更难实现
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Rails 4 为您提供了一种完成相同任务的方法:
Artist.where('lower(name) = ?', name.downcase).first_or_create(:name=>name)
Rails 4 gives you a way to accomplish the same thing:
Artist.where('lower(name) = ?', name.downcase).first_or_create(:name=>name)
此答案是针对问题评论中提出的其他问题的。
如果您重写该方法,您将无法调用默认的
find_or_create_by_name
。但您可以实现自己的方法,如下所示:现在您可以调用重写的方法,如下所示:
This answer is for the additional questions asked in the question comments.
You wont be able to call the default
find_or_create_by_name
if you override that method. But you can implement your own as shown below:Now you can call the overridden method as follows:
您必须根据数据库创建索引。
postgreSQL
在
artist_name
列上创建小写索引。mySQL
搜索不区分大小写
sqlLite
使用整理参数在
artist_name
列上创建索引现在您可以以与数据库无关的方式使用 find_or_create:
参考
PostgreSQL:不区分大小写的搜索
sqlLite:不区分大小写的搜索
You have to create an index based on the database.
postgreSQL
Create a lower case index on
artist_name
column.mySQL
Searches are case insensitive
sqlLite
Create a index on
artist_name
column with collate parameterNow you can use find_or_create in a DB independent manner:
Reference
PostgreSQL: Case insensitive search
sqlLite: Case insensitive search
此处讨论了这一问题。没有人能想出比你更好的解决方案:)
Talked about this one here. No one was able to come up with a solution better than yours :)