1

今、私はコントローラーで検索ロジックを実行します:

def index
    @title = "All cars"
    #show manufacturers only with cars
    @manufacturers = Manufacturer.select('*')
    @manufacturers = @manufacturers.where('count_cars > ?', 0)

    @cars = Car.select('*')
    #manufacturer
    @cars = @cars.where('manufacturer_id = ?', params[:filter_manufacturer]) if not params[:filter_manufacturer].blank?
    #model
    @models = Model.select('*')
    @models = @models.where('manufacturer_id = ?', params[:filter_manufacturer]) if not params[:filter_manufacturer].blank? and not params[:filter_model].blank?
    @models = @models.where('count_cars > ?', 0)

    @cars = @cars.where('model_id = ?', params[:filter_model]) if not params[:filter_model].blank?
    #years
    @cars = @cars.where('year >= ?', params[:filter_year_from]) if not params[:filter_year_from].blank?
    @cars = @cars.where('year <= ?', params[:filter_year_to]) if not params[:filter_year_to].blank?
    #district
    @cars = @cars.where('district_id = ?', params[:filter_district]) if not params[:filter_district].blank?
    #body type
    @cars = @cars.where('car_body_type_id = ?', params[:filter_body_type]) if not params[:filter_body_type].blank?
    #fuel
    @cars = @cars.where('car_fuel_id = ?', params[:filter_fuel]) if not params[:filter_fuel].blank?
    #transmission
    @cars = @cars.where('car_transmission_id = ?', params[:filter_transmission]) if not params[:filter_transmission].blank?
    #wheel drive
    @cars = @cars.where('car_drive_wheel_id = ?', params[:filter_drive_wheel]) if not params[:filter_drive_wheel].blank?
    #doors
    @cars = @cars.where('doors = ?', params[:filter_doors]) if not params[:filter_doors].blank?
    #without damage
    @cars = @cars.where('damage = ?', false) if params[:filter_damage]
    #with customs
    @cars = @cars.where('customs = ?', true) if params[:filter_customs]
    #not crashed
    @cars = @cars.where('crash = ?', false) if params[:filter_crash]
    #price
    @cars = @cars.where('price >= ?', params[:filter_price_from]) if not params[:filter_price_from].blank?
    @cars = @cars.where('price <= ?', params[:filter_price_to]) if not params[:filter_price_to].blank?

    @cars = @cars.paginate(:page => params[:page], :per_page => 20)
end

このロジックをモデルに移動する必要がありますか?はいの場合は、マークアップを手伝ってください。

4

3 に答える 3

1

はい、このロジックをモデルに移動できます。

そのためのスコープを追加する必要があります

@manufacturers = @manufacturers.where('count_cars > ?', 0)

のようなスコープに変換する

scope :count_car, where("count_cars > ?", 0)

その後、次のように呼び出すことができます

 @manufacturers = @manufacturers.count_car

そのための他のスコープの作成と同じ

また

モデルでメソッドを定義することもできます。たとえば、

@cars = @cars.where('manufacturer_id = ?', params[:filter_manufacturer]) if not params[:filter_manufacturer].blank?

に変換する

def cars(manufacture_id=nil)
  where('manufacturer_id = ?',manufacture_id ) if manufacture_id.present?
end

このメソッドをスコープと同じように呼び出すことができます

于 2012-10-18T10:11:41.100 に答える
0

コントローラー内

@cars = Car.search(params)

車のモデルで

def self.search(params)
    cars = Car.select('*')
    cars = cars.where('manufacturer_id = ?', params[:filter_manufacturer]) if not params[:filter_manufacturer].blank?
    cars = cars.where('model_id = ?', params[:filter_model]) if not params[:filter_model].blank?
    #years
    cars = cars.where('year >= ?', params[:filter_year_from]) if not params[:filter_year_from].blank?
    cars = cars.where('year <= ?', params[:filter_year_to]) if not params[:filter_year_to].blank?
    #district
    cars = cars.where('district_id = ?', params[:filter_district]) if not params[:filter_district].blank?
    #body type
    cars = cars.where('car_body_type_id = ?', params[:filter_body_type]) if not params[:filter_body_type].blank?
    #fuel
    cars = cars.where('car_fuel_id = ?', params[:filter_fuel]) if not params[:filter_fuel].blank?
    #transmission
    cars = cars.where('car_transmission_id = ?', params[:filter_transmission]) if not params[:filter_transmission].blank?
    #wheel drive
    cars = cars.where('car_drive_wheel_id = ?', params[:filter_drive_wheel]) if not params[:filter_drive_wheel].blank?
    #doors
    cars = cars.where('doors = ?', params[:filter_doors]) if not params[:filter_doors].blank?
    #without damage
    cars = cars.where('damage = ?', false) if params[:filter_damage]
    #with customs
    cars = cars.where('customs = ?', true) if params[:filter_customs]
    #not crashed
    cars = cars.where('crash = ?', false) if params[:filter_crash]
    #price
    cars = cars.where('price >= ?', params[:filter_price_from]) if not params[:filter_price_from].blank?
    cars = cars.where('price <= ?', params[:filter_price_to]) if not params[:filter_price_to].blank?
    cars
end
于 2012-10-18T10:16:10.483 に答える
0

すべての @cars.where クエリについては、Ransack として調べてください: https://github.com/ernie/ransack

この Gem を使用すると、パラメーター名に応じてクエリを簡単に作成できます。いくつかの良い例については、彼らの wiki を参照してください: https://github.com/ernie/ransack/wiki/Basic-Searching

于 2012-10-18T10:16:24.817 に答える