艺术家Group_由嵌套属性Order_date [英] Artists Group_by nested attribute Order_date
问题描述
我创建了一个运作良好的电子商务平台,供会员购买歌曲。一切工作正常,但我想将我的所有订单按月分组在我的索引页中。
目前,我可以将每个专辑与其相应的艺术家进行分组,每个有序歌曲到其相应的专辑。但现在我想按月对订单进行分组。
如何在订单表中按照订单日期对艺术家进行分组,以便一切按月组织?
Ex。我喜欢做的事
p
月1订单
艺术家1
专辑1 ###与艺术家相对应的专辑列表
- Song1(10 0rders)
- 歌曲3(5 Orders)
专辑2
- 歌曲5(2 Orders)###与歌曲相对应的歌曲
月份2订单
Artist2
Album1
- Song2(1 Order)
Artist3
Album3
--Song5(1 Order)
MODELS
class Order< ActiveRecord :: Base
attr_accessible:artist_id,:album_id,:song_id,:user_id,:order_date
$ b belongs_to:song
belongs_to:用户
结束
class艺术家< ActiveRecord :: Base
attr_accessible:name
has_many:albums
has_many:songs,:through => :专辑
has_many:orders,:through => :歌曲
结束
类专辑< ActiveRecord :: Base
attr_accessible:name,:artist_id
elongs_to:artist
has_many:songs
has_many:orders,:through => :歌曲
结尾
课程歌曲< ActiveRecord :: Base
attr_accessible:artist_id,:album_id,:title,:price
elongs_to:album
has_many:orders
end
控制器
###如何在订单表中按照订单日期对艺术家进行分组?
$ b def index
artart = Artist.includes(:orders).where('orders.id IS NOT NULL')
end
$ c $
$ b $ p $<%@ artists.each do | artist | %GT; ###列出所有拥有订单的艺术家
< h3><%= artist.name>>< / h3>
<%artist.albums.each do | album | %GT; ###列出与艺术家相对应的所有专辑
< h4><%= album.name%>< / h4>
<%album.songs.each do | song | %GT; ###列出与专辑对应的所有有序歌曲
<%if song.orders.count> = 1%>
(<%= song.orders.count%>)
<%= song.title%>
$<%= song.price%>< br>
<%end%>
<%end%>
<%end%>
<%end%>
解决方案这就是我最终解决问题的方法
控制器
@orders = Order.find(:all,:order =>'order_date,id',:limit => 50)
<%@ orders.sort.group_by {| order | order.order_date.beginning_of_month} .each do | month,orders | %GT;
< h3><%= month.strftime('%B')%> < / H3>
<%orders.group_by {| order | order.song.album.artist} .each do | artist,orders | %GT;
< h4><%= artist.name%> < / H4>
<%orders.group_by {| order | order.song.album}。每个|相册,订单| %GT;
< h5><%= album.name%> < / H5>
<%orders.group_by {| order | order.song} .each do |歌曲,命令| %GT;
< p>(<%= orders.count%>)<%= song.title%> < / p为H.
< p><%= song.price%> < / p为H.
<%end%>
<%end%>
<%end%>
<%end%>
I have created a functioning e-commerce platform where Members can buy songs. Everything works fine, But I would like to group all of my Orders in my Index Page by Month.
Currently I am able to group each Album with its corresponding Artist, and each Ordered Song to its corresponding Album. But now I would like to group Orders by Month.
How Can I Group Artists by the order_date in my Orders Table, So that everything is organized by Month?
Ex. of what I've like to do Month 1 Orders Artist1 Album1 ###List of Albums Corresponding to an Artist --Song1 (10 0rders) --Song3 (5 Orders) Album2 --Song5 (2 Orders) ###Ordered Songs Corresponding to an Album Month 2 Orders Artist2 Album1 --Song2 (1 Order) Artist3 Album3 --Song5 (1 Order)
MODELS
class Order < ActiveRecord::Base attr_accessible :artist_id, :album_id, :song_id, :user_id, :order_date belongs_to :song belongs_to :user end class Artist < ActiveRecord::Base attr_accessible :name has_many :albums has_many :songs, :through => :albums has_many :orders, :through => :songs end class Album < ActiveRecord::Base attr_accessible :name, :artist_id belongs_to :artist has_many :songs has_many :orders, :through => :songs end class Song < ActiveRecord::Base attr_accessible :artist_id, :album_id, :title, :price belongs_to :album has_many :orders end
CONTROLLERS
###How Can I Group Artists by the order_date in my Orders Table? def index @artists = Artist.includes(:orders).where('orders.id IS NOT NULL') end
VIEWS
<% @artists.each do |artist| %> ###Lists All Artists with Orders <h3><%= artist.name %></h3> <% artist.albums.each do |album| %> ###Lists All Albums corresponding to Artist <h4><%= album.name %></h4> <% album.songs.each do |song| %> ###Lists All Ordered Songs corresponding to Albums <% if song.orders.count >= 1 %> (<%= song.orders.count %>) <%= song.title %> $<%= song.price %><br> <% end %> <% end %> <% end %> <% end %>
解决方案This is how I ended up Solving the Problem
Controller
@orders = Order.find(:all, :order => 'order_date, id', :limit => 50)
Views
<% @orders.sort.group_by { |order| order.order_date.beginning_of_month }.each do |month, orders| %> <h3><%= month.strftime('%B') %> </h3> <% orders.group_by { |order| order.song.album.artist }.each do |artist, orders| %> <h4><%= artist.name %> </h4> <% orders.group_by { |order| order.song.album }.each do |album, orders| %> <h5><%= album.name %> </h5> <% orders.group_by { |order| order.song }.each do |song, orders| %> <p>(<%= orders.count %>) <%= song.title %> </p> <p><%= song.price %> </p> <% end %> <% end %> <% end %> <% end %>
这篇关于艺术家Group_由嵌套属性Order_date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!