我已尝试this显示记录并成功显示如下内容:
然后,我尝试使用属性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 %>
我的记录表如下:
我想显示totalsold
,例如:
何时创建具有相同日期,nomor和store的totalsolds,qty不计算的问题。 :