2012-02-13 18 views
-1
class Anketum < ActiveRecord::Base 
    has_one :user 

    class << self 
    def search(params) 
     self.scope :h, :conditions => {:height => params[:height]} 
     #scope :w, :conditions => {:width => params[:width]} 
     self.h if params[:height] 
    end 

    end 
end 

はのparamsに依存レール[:XXX]:複数のスコープは、私は、複数のスコープを作成する必要が

# app/models/anketum.rb 
class Anketum < ActiveRecord::Base 
end 

# app/controller/some_controller.rb 
def search 
    @results = Anketum.scoped 
    [:width, :height, :any, :other, :searchable, :attribute].each do |key| 
    @results.where(key => params[key]) if params[key].present? 
    end 
end 

あなたの存在

答えて

1

は、あなたが道、このオーバー・エンジニアリングしている、あなたのコードサンプルから判断しますモデルは決してparamsハッシュにアクセスするべきではありません。

+0

奇妙なSELECT "anketa"。* FROM "anketa" ' –

+0

dbテーブルは" anketa "とも呼ばれますか?そうでない場合は、セットアップに非常に間違ったことがあります。 – coreyward

+0

あなたはどちらのバージョンのレールですか?私は 'named_scope'の代わりに' scope'を使っているので3と仮定しました。 – coreyward