Ruby-on-rails 以 DESC 的身份订购一个系列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3976295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:26:08  来源:igfitidea点击:

Order a collection as DESC

ruby-on-railsruby

提问by Soroush Hakami

<%= render :partial => 'event', :collection => @events.sort_by(&:event_at)%>

This code shows a collection ordered as ASC, but i want to order this collection as DESC.

此代码显示了一个按 ASC 排序的集合,但我想将此集合排序为 DESC。

How can I achieve this?

我怎样才能做到这一点?

回答by Yannis

Even better, you can set a scope to sort your event and use it in your render.

更好的是,您可以设置一个范围来对事件进行排序并在渲染中使用它。

In your Event model:

在您的事件模型中:

scope :desc, order("events.event_at DESC")

If you use Rails3, in your view you can simply do:

如果您使用 Rails3,在您看来,您可以简单地执行以下操作:

<%= render @events.desc %>

回答by Francisco DC

回答by JosephL

In Rails 3 the correct syntax is:

在 Rails 3 中,正确的语法是:

<%= render :partial => 'event', :collection => @events.order(:event_at).reverse_order %>

回答by Shadwell

You can just reverse the sorted collection:

您可以反转已排序的集合:

<%= render :partial => 'event', :collection => @events.sort_by(&:event_at).reverse %>

but as Yannis says you are better off sorting as you fetch things from the database ideally.

但正如 Yannis 所说,最好在从数据库中获取内容时进行排序。

回答by Damien MATHIEU

Depending of the kind of object you have, you will have different ways of doing the sort function.

根据您拥有的对象类型,您将有不同的方式来执行排序功能。

If your object is an ActiveRecord, you can do it the following way:

如果您的对象是 ActiveRecord,您可以通过以下方式进行操作:

@events.order('events.event_at DESC')

This will add an ORDERclause to your SQL query, sorting the entries before you retrieve them from the database.

这将向ORDER您的 SQL 查询添加一个子句,在您从数据库中检索条目之前对条目进行排序。

The second solution is slower, as you're sorting your entries in ruby.
But if you're manipulating an array of objects, it's your only solution.

第二种解决方案较慢,因为您在 ruby​​ 中对条目进行排序。
但是,如果您要操作一组对象,则这是唯一的解决方案。

@events.sort {|a,b| b.event_at <=> a.event_at }

This will loop through all the events, checking each one of them for the biggest one with the <=>method.

这将遍历所有事件,使用该<=>方法检查每个事件中最大的事件。

You can also see the sort documentationon Enumerables.

您还可以查看Enumerables 上的排序文档

回答by Peter Dawson

I wanted to display a league table and order by points desc. After trying out several unsuccesful methods this is what worked for me in this instance. I added this line to my controllers index method.

我想显示一个联赛表并按点数降序排列。在尝试了几种不成功的方法之后,这就是在这种情况下对我有用的方法。我将此行添加到我的控制器索引方法中。

@teams = Team.all.order(points: :desc)

回答by Amol Udage

You can do this by using descmethod with parameter.

您可以通过使用desc带参数的方法来做到这一点。

See below example

见下面的例子

@events.desc(:event_at)

This will give you @events in descending order of event_atfield.

这将按event_at字段的降序为您提供@events 。

Thanks.

谢谢。