我希望能够执行
如何才能最好地实现这一点呢?目前,我只是直接向
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)
,但是这似乎更难实现。
Artist.find_or_create_by_name(artist_name, :case_sensitive => false)
更加困难? - Harish Shettyfind_or_create_by_name
是由method_missing
动态创建的吗?也许这并不难 -- 你会如何实现它? - Tom Lehmanmethod_missing?
。 - Harish Shetty:case_sensitive => false
,我该如何访问默认的find_or_create_by_name
行为呢?(我猜复制它并不是什么大问题) - Tom Lehman