Ruby on rails 3 如何将postgresql列转换为浮动,以便在rails中进行范围搜索
我正在尝试在rails中搜索我的postgresql数据库。我遵循了教程,它正在以纯文本的形式为“我的项目”列的名称和类别工作。然而,我想在我的搜索中设置一个最小/最大价格,这就是我遇到的问题。在my db中,我的价格以字符串形式存储,格式为“AU$49.95”。我可以在范围搜索中将其转换为动态浮动吗?如果是,怎么做?如果没有,我该怎么办 代码如下: search.rbRuby on rails 3 如何将postgresql列转换为浮动,以便在rails中进行范围搜索,ruby-on-rails-3,postgresql,full-text-search,Ruby On Rails 3,Postgresql,Full Text Search,我正在尝试在rails中搜索我的postgresql数据库。我遵循了教程,它正在以纯文本的形式为“我的项目”列的名称和类别工作。然而,我想在我的搜索中设置一个最小/最大价格,这就是我遇到的问题。在my db中,我的价格以字符串形式存储,格式为“AU$49.95”。我可以在范围搜索中将其转换为动态浮动吗?如果是,怎么做?如果没有,我该怎么办 代码如下: search.rb class Search < ActiveRecord::Base attr_accessible :keyword
class Search < ActiveRecord::Base
attr_accessible :keywords, :catagory, :minimum_price, :maximum_price
def items
@items ||= find_items
end
private
def find_items
scope = Item.scoped({})
scope = scope.scoped :conditions => ["to_tsvector('english', items.name) @@ plainto_tsquery(?)", "%#{keywords}%"] unless keywords.blank?
scope = scope.scoped :conditions => ["items.price >= ?", "AU \$#{minimum_price.to_s}"] unless minimum_price.blank?
# scope = scope.scoped :conditions => ["items.price <= ?", "AU \$#{maximum_price.to_s}"] unless maximum_price.blank?
scope = scope.scoped :conditions => ["to_tsvector('english', items.catagory) @@ ?", catagory] unless catagory.blank?
scope
end
end
类搜索[“to_-tsvector('english',items.name)@@plainto_-tsquery(?),“%#{keywords}%”除非关键字。空白?
scope=scope.scope:conditions=>[“items.price>=?”,“AU\${minimum_price.to_s}]除非minimum_price.blank?
#scope=scope.scope:conditions=>[“items.price使用数据类型或进行精确的数值计算,无舍入错误-并将排序作为数字(而不是文本)。
将字符串文字转换为数值
不应该是一个性能问题。最好不要将数字存储为字符串。即使找到了解决方法,将大量数据转换为浮点值也会对性能造成影响。好的,谢谢。也许最好的做法是更改数据库。我在存储数据时很懒。它现在又回来咬我了哇,
class SearchesController < ApplicationController
def new
@search = Search.new
end
def create
@search = Search.new(params[:search])
if @search.save
redirect_to @search, :notice => "Successfully created search."
else
render :action => 'new'
end
end
def show
@search = Search.find(params[:id])
end
end