首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何不在rails 4上的ruby上显示副本?

如何不在rails 4上的ruby上显示副本?
EN

Stack Overflow用户
提问于 2015-08-22 22:21:10
回答 2查看 115关注 0票数 2

我正在开发一个应用程序,用户可以从youtube上传视频,但用户可以从同一个艺术家那里多次上传不同的歌曲。我只需要它展示艺术家的一首歌。现在这是我得到的结果。"Sensato“被放入两个不同的组,即使是相同的名字的艺术家。

集团市长-德巴拉塔

集团 El Alfa - jevi

集团未来-其中ya

集团 Sensato - Se Feliz

集团 Sensato - Bello

(控权人)

代码语言:javascript
运行
复制
def index
    @items = Video.all.order("cached_votes_up DESC, artist ASC")
    @items_by_day = @items.group_by { |t| t.artist }

end

(见图)

代码语言:javascript
运行
复制
    <% @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 %>

(模型)

代码语言:javascript
运行
复制
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

(移徙)

代码语言:javascript
运行
复制
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

EN

回答 2

Stack Overflow用户

发布于 2015-08-22 22:34:45

你需要这样的东西:

代码语言:javascript
运行
复制
@items_by_day = @items.select('artist').uniq.group('artist')

更新

试试这个:

代码语言:javascript
运行
复制
def index
  @items = Video.all.order("cached_votes_up DESC, artist ASC")
  @items_by_day = Video.select('artist').uniq.group('artist').joins('videos')
end
票数 0
EN

Stack Overflow用户

发布于 2015-08-22 22:37:10

我认为您可能正在寻找这个(使用 method):

代码语言:javascript
运行
复制
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散列,而不是字符串,如果可能的话,这确实是首选方法。)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32161358

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档