0
나는 여러 가지 기준에 따라 자동차를 필터링 할 수있는 내 사이트 페이지가 있습니다. 이러한 기준은 매개 변수를 통해 전달됩니다.Rails 방법을 체인 가능 범위로 변환
새 조건을 추가하고 싶지만이 작업을 수행하는 모델의 논리는 방법입니다. Active Record 쿼리 및 체인 가능한 부분을 기반으로하므로 논리를 필터링하는 컨트롤러에 추가 할 수 없습니다.
다음 방법을 필터 논리의 쿼리 및 체인과 호환되는 것으로 변환하려면 어떻게해야합니까?
방법 :
def self.new_cars_in_year(year)
new_cars = []
Car.all.includes(:drives).each do |car|
years_driven_car = []
c = car.drives.where("date IS NOT ?", nil)
c.each do |drive|
years_driven_car << (Date.parse drive.date).year
end
years_driven_car = years_driven_car.uniq
if car.unknown_drives
years_driven_car.shift
end
if years_driven_car.min == year
new_cars << car
end
end
new_cars
end
스키마 :
ActiveRecord::Schema.define(version: 20170816154910) do
create_table "cars", force: :cascade do |t|
t.text "notes", limit: 65535
t.datetime "created_at"
t.datetime "updated_at"
t.string "slug", limit: 255
t.string "covering", limit: 255
t.text "style", limit: 65535
t.text "model", limit: 65535
t.float "speed", limit: 24
t.boolean "unknown_drives"
end
create_table "drives", force: :cascade do |t|
t.integer "car_id", limit: 4
t.string "notes", limit: 255
t.string "date", limit: 255
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
감사합니다
편집 :
당신이 당신의 모델 및/또는 스키마를 추가 할 수 있습니다 : 나는 당신의 응용 프로그램에 익숙하지 않은거야하지만,이 일할 수있는
:이 SQL이었다
경우는 같은 것 시나리오를 복제하려고합니까? –
@ SebastiánPalma DB 스키마가 추가되었습니다. 감사 – rctneil