我正在开发一个应用程序,用户可以从youtube上传视频,但用户可以从同一个艺术家那里多次上传不同的歌曲。我只需要它展示艺术家的一首歌。现在这是我得到的结果。"Sensato“被放入两个不同的组,即使是相同的名字的艺术家。
集团市长-德巴拉塔
集团 El Alfa - jevi
集团未来-其中ya
集团 Sensato - Se Feliz
集团 Sensato - Bello
(控权人)
def index
@items = Video.all.order("cached_votes_up DESC, artist ASC")
@items_by_day = @items.group_by { |t| t.artist }
end
(见图)
<% @items_by_day.each do |day, items| %>
<div class="row">
<h1>Group</h1>
<% for a in items %>
<div class="videothumb col-xs-12 col-sm-4">
<div class="" style="
background-size: 100% 100%;
height: 240px;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
overflow: hidden;">
<%= link_to a do %>
<div style="background: rgba(0,0,0, 0); width:100%; height: 85%;z-index:1000; position:absolute; top:0;"></div>
<% end %>
<iframe width="100%" height="100%" src="https://www.youtube.com/embed/<%= a.url %>" frameborder="0" allowfullscreen></iframe>
</div>
<div class="col-xs-12">
<h3>
<%= link_to a.title, a %> </h3>
</div>
<div class="col-xs-6">
<h2><%= a.artist %></h2>
</div>
<div class="col-xs-6">
<div style="text-align:right">
<%= link_to like_video_path(a), method: :put, class: "" do %>
<span class="glyphicon glyphicon-chevron-up">
<%= a.get_upvotes.size %>
<% end %>
<%= link_to dislike_video_path(a), method: :put, class: "" do %>
<span class="glyphicon glyphicon-chevron-down">
<%= a.get_downvotes.size %>
<% end %>
</div>
</div>
</div>
<% end %>
</div>
<% end %>
(模型)
class Video < ActiveRecord::Base
acts_as_votable
belongs_to :user
validates :title,
presence: true,
length: { minimum: 3 },
uniqueness: true
validates :url,
presence: true,
length: { minimum: 3 },
uniqueness: true
validates :genre, presence: true
end
(移徙)
class CreateVideos < ActiveRecord::Migration
def change
create_table :videos do |t|
t.string :title
t.string :artist
t.string :url
t.text :description
t.timestamps null: false
end
end
end
最终结果应该如下所示
集团市长-德巴拉塔
集团 El Alfa - jevi
集团未来-其中ya
集团 Sensato - Se Feliz
发布于 2015-08-22 22:34:45
你需要这样的东西:
@items_by_day = @items.select('artist').uniq.group('artist')
更新
试试这个:
def index
@items = Video.all.order("cached_votes_up DESC, artist ASC")
@items_by_day = Video.select('artist').uniq.group('artist').joins('videos')
end
发布于 2015-08-22 22:37:10
我认为您可能正在寻找这个(使用 method):
def index
@items = Video.all.order(cached_votes_up: :desc, artist: :asc)
@items_by_day = Video.select(:title, 'MAX(cached_votes_up) as cached_votes_up', 'MIN(id) as id').group(:artist)
end
(另外,请注意,对于order
参数,我使用的是ruby散列,而不是字符串,如果可能的话,这确实是首选方法。)
https://stackoverflow.com/questions/32161358
复制相似问题