Rails中的Sum和Multiple Group by

时间:2014-05-03 13:22:58

标签: ruby-on-rails ruby-on-rails-3.2

我已尝试this显示记录并成功显示如下内容:

views

然后,我尝试使用属性store添加name模型,因此我的模型的关系是:

class Product < ActiveRecord::Base
  attr_accessible :name
  has_many :totalsolds
end

class Store < ActiveRecord::Base
  attr_accessible :name
  has_many :totalsolds
end

class Totalsold < ActiveRecord::Base
  attr_accessible :date, :nomor ,:qty, :product_id, :store_id, :product_attributes, :store_attributes
  belongs_to :product
  belongs_to :store
  accepts_nested_attributes_for :product
  accepts_nested_attributes_for :store
end

控制器:

@totalsolds = Totalsold.by_month(field: "date").includes(:product).group_by{ |s| [s.date, s.store, s.nomor] }

观点:

<% @totalsolds.each do |(date, store, nomor), totalsold| %>
   <tr>
      <td><%= date.strftime("%d/%m/%Y") %></td>
      <td><%= nomor %></td>
      <td><%= Store.find(store).name %></td>
        <% totalsold.each do |prod| %>
          <td><%= prod.qty %></td> 
        <% end %>
      <td></td>
   </tr>
<% end %>

我的记录表如下:

enter image description here

我想显示totalsold,例如:

enter image description here

何时创建具有相同日期,nomor和store的totalsolds,qty不计算的问题。 :

enter image description here

0 个答案:

没有答案