Ruby-on-rails Rails 按关联模型排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1127192/
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
Rails order by in associated model
提问by Bryan Ward
I have two models in a has_many relationship such that Log has_many Items. Rails then nicely sets up things like: some_log.itemswhich returns all of the associated items to some_log. If I wanted to order these items based on a different field in the Items model is there a way to do this through a similar construct, or does one have to break down into something like:
我在 has_many 关系中有两个模型,这样 Log has_many Items。Rails 然后很好地设置了以下内容:some_log.items它将所有关联的项目返回到 some_log。如果我想根据 Items 模型中的不同字段对这些项目进行排序,是否有一种方法可以通过类似的构造来做到这一点,或者是否必须分解为以下内容:
Item.find_by_log_id(:all,some_log.id => "some_col DESC")
回答by Greg Campbell
There are multiple ways to do this:
有多种方法可以做到这一点:
If you want all calls to that association to be ordered that way, you can specify the ordering when you create the association, as follows:
如果您希望以这种方式对该关联的所有调用进行排序,则可以在创建关联时指定排序,如下所示:
class Log < ActiveRecord::Base
has_many :items, :order => "some_col DESC"
end
You could also do this with a named_scope, which would allow that ordering to be easily specified any time Item is accessed:
您还可以使用 named_scope 执行此操作,这将允许在访问 Item 时轻松指定排序:
class Item < ActiveRecord::Base
named_scope :ordered, :order => "some_col DESC"
end
class Log < ActiveRecord::Base
has_many :items
end
log.items # uses the default ordering
log.items.ordered # uses the "some_col DESC" ordering
If you always want the items to be ordered in the same way by default, you can use the (new in Rails 2.3) default_scope method, as follows:
如果您总是希望默认情况下以相同的方式对项目进行排序,则可以使用(Rails 2.3 中新增的)default_scope 方法,如下所示:
class Item < ActiveRecord::Base
default_scope :order => "some_col DESC"
end
回答by lfender6445
rails 4.2.20syntax requires calling with a block:
rails 4.2.20语法需要使用块调用:
class Item < ActiveRecord::Base
default_scope { order('some_col DESC') }
end
This can also be written with an alternate syntax:
这也可以用替代语法编写:
default_scope { order(some_col: :desc) }
回答by erik
Either of these should work:
其中任何一个都应该有效:
Item.all(:conditions => {:log_id => some_log.id}, :order => "some_col DESC")
some_log.items.all(:order => "some_col DESC")
回答by Sayuj
set default_scopein your model class
default_scope在您的模型类中设置
class Item < ActiveRecord::Base
default_scope :order => "some_col DESC"
end
This will work
这将工作
回答by ToTenMilan
order by direct relationship has_many :model
按直接关系订购 has_many :model
order by joined relationship has_many :modelable, through: :model
按连接关系排序 has_many :modelable, through: :model
class Tournament
has_many :games # this is a join table
has_many :teams, through: :games
# order by :name, assuming team has this column
def teams
super.order(:name)
end
end
Tournament.first.teams # are returned ordered by name

