我希望用户能够使用这个函数搜索我的数据库:
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"。
上面的函数不起作用,但是,我想知道我做错了什么。
谢谢你的帮助
为什么要用"like"来比较数字?你应该使用"="或">="等。
def self.search(query)
new_query = (query.to_d * 100).to_i
where("price_in_cents = ?", new_query)
end
好的,我最后用了这个:
def self.search(query)
string = query.to_s.gsub('.','')
where("amount_in_cents LIKE ?", "%#{string}%")
end
谢谢@grotori让我走上正轨!