【问题标题】:List and Group Orders by Artist and Songs按艺术家和歌曲列出和分组订单
【发布时间】:2014-07-13 13:17:15
【问题描述】:

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

目前我可以将每个作者与其对应的专辑分组,但每首订购的歌曲随后会在每张专辑下多次列出。

如何在不复制歌曲标题和提供显示总订单数的计数器的情况下列出与专辑对应的所有已订购歌曲?

EX. of what I'd like in my Orders Page

###Orders grouped by Artists (which I've Done)
Artist1
  Album1                      ###List of Albums Corresponding to an Artist 
    --Song1 (10 0rders)          (which I've Done)
    --Song3 (5 Orders)
  Album2 
    --Song5 (2 Orders)        ###Ordered Songs Corresponding to an Album (NEED HELP)
  Album3                      ###No Duplicates, only list ordered songs with a counter 

Artist2
  Album1 
    --Song2 (1 Orders)
  Album2 

Artist3
  Album1 
    --Song4 (1 Orders)

模型

class Order < ActiveRecord::Base
  attr_accessible :artist_id, :album_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

控制器

def index    
  @artists = Artist.includes(:orders).where('orders.id IS NOT NULL')    
end

观看次数

<% @artists.each do |artist| %>   ###Lists All Artista with Orders
  <h3><%= artist.name %></h3>

  <% artist.albums.each do |album| %>   ###Lists all Albums Corresponding to Artist
      <h4><%= album.name %></h4>

     <% album.orders.each do |order| %> ###Lists All Ordered Song Titles & Price
       <%= order.song.title %><br>      ###How can i list each ordered song with a counter
       <%= order.song.price %><br>
     <% end %>

  <% end %>

<% end %>

【问题讨论】:

    标签: ruby ruby-on-rails-3 sorting include nested-loops


    【解决方案1】:

    这是未经测试的,但可能值得一试...

    使用group_by 创建由song.title 分组的album.orders 数组。循环遍历哈希。仅输出每个散列的第一个元素的标题和价格。计算哈希中的数组元素,得到这首歌的计数器。

    <% orders_hash = album.orders.group_by{|o|o.song.title} %>
    
    <% orders_hash.each do |order_array| %>
      <%= order_array[0].song.title %>
      <%= order_array[0].song.price %>
      <%= order_array.count %>
    <% end %>
    

    【讨论】:

    • 如果我不将 song.title 添加到 order_array 似乎可以工作,否则我会收到此错误 undefined method `song' for "title name":String
    • order_array 应该是一个完整订单的数组......并且每个订单都属于歌曲并且歌曲有一个属性标题,所以order_array.first.song.title(或order_array[0].song.title)应该可以工作。如果你可以“撬”,你可能想检查 orders_hash 是一个哈希,键是歌曲标题,值是订单对象的数组。
    【解决方案2】:

    这是我想出的解决方案

       <% @artists.each do |artist| %>
         <h3><%= artist.name %></h3>
    
         <% artist.albums.each do |album| %>
           <h4><%= album.name %></h4>
    
             <% album.songs.each do |song| %>
    
               <% if song.orders.count >= 1 %>
                 (<%= song.orders.count %>)
                 <%= song.title %>
                 $<%= song.price %><br>
               <% end %>
    
             <% end %> 
    
    
         <% end %>
    
       <% end %>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-22
      • 1970-01-01
      • 2011-05-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多