Railsでpostgresqlデータベースを検索しようとしています。Railscasts#111 Advanced Searchチュートリアルに従いましたが、プレーンテキストのアイテム列の名前とカテゴリで機能しています。ただし、検索でも最小/最大価格を設定したいので、そこで問題が発生します。私のデータベースでは、私の価格は「AU$49.95」の形式の文字列として保存されています。スコープ検索でこれをオンザフライでフロートに変換できますか?もしそうなら、どのように?そうでない場合は、どうすればよいですか?
コードは次のとおりです。
search.rb
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
searchs_controller.rb
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
ここまで読んでくれてありがとう!