1

我有这个查询:

@listings = Listing.where('city != ?', 'SommerVille')

我想知道是否有人对可能导致这种情况的其他原因有任何建议?

我只想从城市!=SommerVille 的数据库中获取那些列表。

谢谢


编辑上述问题是由于城市名称中的前导或尾随空格。所以我想知道查询数据库的最佳方法是什么,它忽略大小写,也忽略前导/尾随空格。

以下方法有效,但有更好的方法吗?

@unwanted_cities = Listing.where(['city != ?', "SommerVille"]).where(['city != ?', " SommerVille"]).where(['city != ?', "SommerVille "])
4

1 回答 1

0

我认为大多数 dbs 都支持 trim

@listings = Listing.where('TRIM(city) != ?', 'SommerVille')

从长远来看,在进入数据库的过程中去除空格会更好

忽略大小写的一种方法,使用大写

@listings = Listing.where('UPPER(TRIM(city)) != UPPER(?)', 'SommerVille')

或者

@listings = Listing.where('UPPER(TRIM(city)) != ?', 'SommerVille'.upcase)
于 2012-07-01T23:59:25.207 回答