Ruby-on-rails 如何在 Rails 3 中订购包含的元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5348116/
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
How to order included elements in Rails 3
提问by royvandewater
I have a model relationship where todayhas many tasks
我有一个模型关系,其中today有很多tasks
I'm trying to retrieve a user's todayobject, include the tasksand render them all to Json. All of this was going great until I decided I wanted to order the taskswithin the todayobject because respond_with blockis also used for rendering the html page. Is there any way to include the tasksand order them?
我正在尝试检索用户的today对象,包括tasks并将它们全部呈现给 Json。所有这一切都很顺利,直到我决定要tasks在today对象内订购 ,因为respond_with block它也用于呈现 html 页面。有什么方法可以包含tasks和订购它们吗?
I'm trying something like this:
我正在尝试这样的事情:
class TodaysController < ApplicationController
respond_to :html, :json
def show
@today = Today.where(:user_id => current_user.id).joins(:tasks).includes(:tasks).order(:priority).first
respond_with @today, :include => :tasks
end
end
This retrieves everything correctly, but does not seem to order the tasks at all.
这将正确检索所有内容,但似乎根本没有对任务进行排序。
This is what I used to have (which worked great, but didn't have the ordering):
这是我曾经拥有的(效果很好,但没有订购):
class TodaysController < ApplicationController
respond_to :html, :json
def show
@today = current_user.today
respond_with @today, :include => :tasks
end
end
I know I can retrieve the data and sort it afterwards like this:
我知道我可以像这样检索数据并在之后对其进行排序:
@today = current_user.today
@today.tasks.sort!{|a,b| a.priority <=> b.priority }
This works and will pass my tests, but I was hoping for an ActiveRecord way to solve this.
这有效并且将通过我的测试,但我希望有一种 ActiveRecord 方法来解决这个问题。
回答by khelll
Try this in your Todaymodel:
在你的Today模型中试试这个:
has_many :tasks, :order => 'priority DESC'
EDIT: As mentioned in comment below, in Rails 4+, this is now:
编辑:正如下面评论中提到的,在 Rails 4+ 中,现在是:
has_many :tasks, -> { order(:priority => :desc) }
(更多信息在这里)
回答by Hyman
Direct solution would be to include the taskstable name before priority:
直接的解决方案是在tasks之前包含表名priority:
Today.where(:user_id => current_user.id).includes(:tasks).order('tasks.priority').first
# joins(:tasks) is not required
Or, if you don't want to have the table name hardcoded, you can merge with scope from Taskmodel:
或者,如果您不想对表名进行硬编码,则可以与Task模型中的范围合并:
Today.where(:user_id => current_user.id).joins(:tasks).includes(:tasks).merge(Task.order(:priority)).first
# joins(:tasks) here is required
Also, you can add has_many: todaysto Usermodel to ditch the whereclause and do:
此外,您可以添加has_many: todays到User模型以放弃该where条款并执行以下操作:
current_user.todays.includes(:tasks).order('tasks.priority').first
# or
current_user.todays.joins(:tasks).includes(:tasks).merge(Task.order(:priority)).first
But if you need only/always to order by priority, and do not need other different orderings, adding orderto has_many :tasksis easier.
但是,如果你只需要/总是顺序按优先级,并且不需要其他不同的排序,加入order到has_many :tasks更加容易。

