현재 달에 게재되는 모든 광고 주문을 반환하는 다음과 같은 방법이 있습니다. 주문 has_many newspaper_placements. 주문에 호출되는 이야기 해봐 후 연관 개수가 0보다 큰 개체를 반환하려면 Rails .where를 사용하십시오.
def self.orders_with_placements_in_current_month(all_cc_mc)
filter_start = Date.today.beginning_of_month
filter_end = Date.today.end_of_month
all_orders = []
Order.where(client_companies_media_company_id: all_cc_mc).each do |x|
if x.newspaper_placements.count > 0
if (filter_start..filter_end).overlaps?(x.newspaper_placements.first.date..x.newspaper_placements.last.date)
all_orders << x
end
end
end
end
, 나는 newspaper_placement 카운트가 0보다 큰 경우 반환 게재 위치가없는 경우를 다음과 코드가 실패 할 것이기 때문에, 볼 수있는 조건을 포함하고있다. 원래 쿼리에서 newspaper_placement 개수가 0보다 큰 주문 만 반환 할 수있는 방법이 있습니까? 따라서 내가하고 싶은 일은 다음과 같습니다.
Order.where(client_companies_media_company_id: all_cc_mc)
게재 순위 수가 0보다 큰 주문 만 반환하므로 따르는 조건을 제거 할 수 있습니다.
중복 가능성 ([레일 협회의 개수의 쿼리 조건 3] http://stackoverflow.com/questions/6313621/rails-3-query-on-condition- of-association-count) – sebkkom