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 回答