2011-03-27 2 views
0

간단한 주문 - 아이템 앱을 가지고 있습니다. 카트가 없습니다. 내 보고서의 조인 된 범위에 대한 도움이 필요합니다.Rails3 조인 범위 도움말

제품, 서비스 및 정책으로 항목의 다형성 (belongs_to :itemable, :polymorphic => true)입니다. 정책은 공급 업체에 속합니다.

목표는 판매자가 얼마만큼 팔았는지 나와 있지만 아래의 주문 범위를 사용할 수 있습니다. 주문 모델에서 추측 - 같은

스코프 내 주문 모델

scope :closed_today, lambda { where("closed_date IS NOT ? AND closed_date >= ? AND closed_date < ?", nil, Time.now.midnight, Time.now.midnight.tomorrow)} 
scope :closed_yesterday, lambda { where("closed_date IS NOT ? AND closed_date >= ? AND closed_date < ?", nil, Time.now.midnight.yesterday, Time.now.midnight)} 
... 

뭔가? 공급 업체 그룹에 수있는 및 금액은 판매 얻을

scope :with_policies, joins(:items).where(:itemable_type => "Policy") 

그리고 최종 결과.

<% for v in @orders.closed_today.with_policies.group("itemable.vendor_id") %> 
    <%= v.somehow_get_to.vendor.name %> Sold: <%= v.somehow_get_to.collect.sum(&:price) %> 

가격은 해당 품목의 판매량을 유지하는 항목 필드입니다.

위의 그림과 같이 그룹화 할 수 없다는 것을 알고 있으므로 어떻게해야하는지 알려주는 것이 좋습니다. 내 Items 테이블에 vendor_id를 추가하지 않으시겠습니까?


UPDATE 여기

는 내가하고 결국 것입니다. 항목에 vendor_id를 추가했습니다. 내 인생을 더 쉽게 만들었습니다. 이것이 더 나은 방법이라고 확신합니다. 내보기에서

scope :in_orders_with, lambda { |orders, type| where('itemable_type IS ?', type).joins(:order) & orders } 

내 항목 모델에서

(@ orders.closed_today 때문에 실제로 다른보기에서 전달됩니다.)

<% orders = @orders.closed_today %> 
<p> 
<% @items = Item.in_orders_with(orders, "Policy") %> 
<% if [email protected]? %> 
    <% @items.group_by{ |o| o.vendor }.each do |vendor, items| %> 
    <%= vendor.name %> <br />Qty Sold:<%= items.count %> Dollars Sold:<%= items.collect.sum(&:price) %><br /><br /> 
    <% end %> 
<% else %> 
    Nothing to report from Policies<br /> 
<% end %> 
</p> 

답변

0
<% @orders.closed_today.with_policies.group_by{|o| o.itemable.vendor}.each do |vendor, orders| %> 
    <%= vendor.name %> SOLD: <%= orders.sum(&:price) %> 
<% end %> 
+0

item_table는 무엇을 할 것인가? 정의되지 않은 오류가 발생합니다. – pcasa

+0

'itemable' sorry :) – fl00r

+0

너무 가까이. o.itemable.vendor가 작동하지 않습니다. o.items.last.itemable.vendor와 같이 강제 실행해야합니다. – pcasa