简体   繁体   English

Z = X + Y时如何防止堆叠顺序? X总是在最前面

[英]how to prevent stacked ordering when I do Z=X+Y? X is always on top

Here's what I mean 这就是我的意思

This is my controller 这是我的控制器

class StaticPagesController < ApplicationController

  def home
    if signed_in?
      @post_items = current_user.posts
      @activities = PublicActivity::Activity.order("created_at desc")
      @items = @post_items + @activities
      @items.sort_by{|item| item.class == PublicActivity::Activity ? item.created_at : item.created_at}
      @items = @items.paginate(:page => 1, :per_page => 20)
    else
    redirect_to root_path  
    end
  end

As you can see in the above, this line @items = @post_items + @activities causes stacked ordering. 正如您在上面看到的那样,此行@items = @post_items + @activities导致堆叠排序。 All of the post_items are ordered first THAN the activities are ordered below it. 所有的post_items都比其下的活动先被排序。

I am trying to combine BOTH into one ordering using "created_at". 我正在尝试使用“ created_at”将两者合并为一个顺序。

How can I prevent this stacked ordering and make it order as one? 如何防止这种堆叠式订购并将其作为一体订购? Thanks 谢谢

sort_by isn't doing what you think it's doing. sort_by并未按照您认为的去做。 It's returning a new sorted array. 它返回一个新的排序数组。 If you want to modify the array in-place, you have to use sort_by! 如果要就地修改数组,则必须使用sort_by! :

@items.sort_by!(&:created_at)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何查询 ActiveRecord 以仅返回每个 object 的第一个实例,其中 enum = X、Y、Z? - How do I query ActiveRecord to return only the first instance of each object where enum = X, Y, Z? Rails3:如果X属于Y,如何显示所有X,而与Y无关? - Rails3: If X belong_to Y, how do I display all X regardless of Y? 如何创建{x = y | y = z | z = 0}从哈希动态阻止? - How to create { x = y | y = z | z = 0 } block dynamically from hash? 如何在ActiveRecord准备好的语句中使ruby数组为ax,y,z - how to make a ruby array be a x,y,z in an ActiveRecord prepared statement 模型X和模型Y每个has_many Z,:dependent =&gt;:destroy。 如果我摧毁X,Z会被摧毁吗? - Model X and Model Y each has_many Z, :dependent => :destroy. If I destroy X, does Z get destroyed? [X,Y,Z] .each {| m |的区别是什么? 包括m}并包括X,Y,Z? - What is the difference between [X,Y,Z].each {|m| include m} and include X, Y, Z? 只要X为真,则X,否则为Y - Whenever X is true do X, otherwise Y 会话未创建例外:使用Selenium Webdriver和Chrome时,Chrome版本必须&gt; = xyz - Session not created exception: Chrome version must be >= x.y.z when using Selenium Webdriver with Chrome 如何根据滑轨中的(表Y /列Z)定义(表A /列X)? - How to define (table A/column X) according to (Table Y/column Z) in rails? 如何在Ruby on Rails中创建指向字段X包含Y的记录列表的链接? - How do I create a link to a list of records where field X contains Y in Ruby on Rails?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM