【问题标题】:Artists Group_by nested attribute Order_date艺术家 Group_by 嵌套属性 Order_date
【发布时间】:2014-05-25 00:07:46
【问题描述】:

我创建了一个功能强大的电子商务平台,会员可以在其中购买歌曲。一切正常,但我想按月在我的索引页面中对我的所有订单进行分组。

目前,我可以将每张专辑与其对应的艺术家分组,并将每首订购歌曲与其对应的专辑分组。但现在我想按月份对订单进行分组。

如何按订单表中的 order_date 对艺术家进行分组,以便按月份组织所有内容?

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)

模型

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

控制器

###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

观看次数

   <% @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 %>

【问题讨论】:

    标签: ruby ruby-on-rails-3 loops group-by nested-loops


    【解决方案1】:

    您可以通过此行获得订单列表:

    <% @order_months.sort.each do |month, orders| %>
    

    然后你又跳回到你的完整艺术家列表中:

       <% @artists.each do |artist| %>             ###Lists All Artists with Orders
    

    相反,我认为您需要类似的东西

       <% orders.each do |order| %>
         <% order.artists.each do |artist| %>
           <% artist.albums.each do |album| %> 
             <% album.songs.each do |song| %>
                 <%= song.name %>
    

    要获得 order.artists,您需要修改模型:

    class Order < ActiveRecord::Base
      attr_accessible :artist_id, :album_id, :song_id, :user_id, :order_date
    
      belongs_to :song
      belongs_to :user
    
      has_many :albums, through: :song
      has_many :artists, through: :albums
    
    end
    

    【讨论】:

    • 出于某种奇怪的原因,这表示 Order 的错误未定义方法“艺术家”。
    • 您需要定义关系。我更新了答案以解释如何。
    • 这行得通,它列出了正确月份下的艺术家。但它仍然显示每个月下的订购歌曲。我认为这是因为我的 if 语句。知道如何解决最后一部分...
    • 嗯,不,它看起来对我来说是正确的。也许检查您的数据是否正确。
    • 你找到问题了吗?
    【解决方案2】:

    这就是我最终解决问题的方式

    控制器

    @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| %>
      <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 %>
    

    【讨论】:

    • 啊,我明白你现在在追求什么了。
    猜你喜欢
    • 1970-01-01
    • 2014-12-02
    • 1970-01-01
    • 1970-01-01
    • 2022-08-03
    • 2017-02-19
    • 1970-01-01
    • 2016-03-01
    • 1970-01-01
    相关资源
    最近更新 更多