0
我的模型中有很大的搜索方法,對應於搜索合適的汽車。 它看起來像這樣:Ruby on Rails:組織大搜索方法
def self.search(params)
cars = joins(:reservations).where.not("reservations.reception_time <= ? AND reservations.return_time >= ?",
params[:return_date], params[:handover_date])
cars = joins(:car_class).where("car_classes.id= ?", params[:car_class])
cars = cars_at_both_locations(params[:handover_location], params[:return_location])
cars = params[:car_body_style] == [""] ? cars : joins(:car_configuration).
where("car_configurations.body_style_id = ?", params[:car_body_style])
cars = params[:car_fuel] == [""] ? cars : where(fuel: params[:car_fuel])
cars = params[:car_transmission] == [""] ? cars : where(transmission: params[:car_transmission])
cars = params [:car_seats] == [""] ? cars : car_seats(params[:car_seats])
cars = Car.joins(:prices).where('prices.to_days >= ?',
(Date.parse(params[:return_date]) - Date.parse(params[:handover_date])).to_i)
end
這是非常不可讀... 有沒有人有任何想法,以重構這個方法是更具可讀性?
聽起來像是一個問題,你應該嘗試在這裏發帖:http://codereview.stackexchange.com/ – Severin
謝謝。我添加了一個帖子: http://codereview.stackexchange.com/questions/51378/ruby-on-rails-refactor-big-search-method –