【发布时间】:2013-07-01 09:56:08
【问题描述】:
我正在使用 Postgres 作为数据库的 Rails 项目中创建搜索功能。
这是我的代码
def self.search(search)
if search
find(:all, :conditions => ["LOWER(name) LIKE LOWER(?) OR LOWER(city) LIKE LOWER(?) OR LOWER(address) LIKE LOWER(?) OR (venue_type) LIKE (?)", "%#{search}%", "%#{search}%", "%#{search}%", "%#{search}%"])
else
find(:all)
end
end
但我的问题是“venue_type”是一个整数。我为venue_type做了一个case switch
def venue_type_check
case self.venue_type
when 1
"Pub"
when 2
"Nattklubb"
end
end
现在我的问题是:当venue_type 是int 时,我如何在查询中找到某些内容?
【问题讨论】:
-
请尝试:find(:all, :conditions => ["LOWER(name) LIKE LOWER(?) OR LOWER(city) LIKE LOWER(?) OR LOWER(address) LIKE LOWER( ?) OR (venue_type = ?)", "%#{search}%", "%#{search}%", "%#{search}%", search.to_i])
-
错误
PG::Error: ERROR: operator does not exist: integer ~~ unknown在哪里被抛出? -
@BachanSmruty 我尝试了你的方式,但是当我搜索“Nattklubb”(venue_type)时,我没有得到任何结果。 ://
-
@rsvmrk,请检查我的答案。希望它对你有用。
标签: ruby-on-rails postgresql search integer