将搜索逻辑从控制器移动到模型

时间:2012-10-18 09:59:06

标签: ruby-on-rails ruby ruby-on-rails-3

现在我在控制器中搜索逻辑:

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

我是否必须将此逻辑移至模型?如果是的话,请帮我标记。

3 个答案:

答案 0 :(得分: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

您可以将此方法称为范围

答案 1 :(得分: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

答案 2 :(得分:0)

对于所有@ cars.where查询构建,请查看Ransack: https://github.com/ernie/ransack

此Gem允许您根据参数名称轻松构建查询。有关一些不错的示例,请参阅他们的wiki:https://github.com/ernie/ransack/wiki/Basic-Searching