我希望用户能够使用此功能搜索我的数据库:
def self.search(query)
new_query = (query.to_d * 100).to_i
where("price_in_cents LIKE ?", new_query)
end
问题是我的所有prices
都作为integers
存储在数据库中,所以如果有人搜索“10.99”,他应该实际搜索“1099”。
上面的功能不起作用,我想知道我做错了什么。
感谢您的帮助。
答案 0 :(得分:2)
为什么使用“like”进行数字比较?您应该使用“=”或“> =”等。
def self.search(query)
new_query = (query.to_d * 100).to_i
where("price_in_cents = ?", new_query)
end
答案 1 :(得分:0)
好的,我最终使用了这个:
def self.search(query)
string = query.to_s.gsub('.','')
where("amount_in_cents LIKE ?", "%#{string}%")
end
感谢@grotori让我走上正轨!