1

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

ここまで読んでくれてありがとう!

4

1 に答える 1

0

データ型を使用するnumericmoney、丸め誤差のない正確な数値計算に使用し、数値として(テキストとしてではなく)並べ替えます。
文字列リテラルをに変換しnumericても、パフォーマンスの問題にはなりません。

于 2012-07-24T13:13:31.187 に答える