【发布时间】:2014-04-19 00:46:26
【问题描述】:
对于我的一个模型,我正在尝试设置一个按年份和季节排序的默认范围。由于 year 是一个整数,因此很容易按此排序。我的麻烦是按季节订购(如果年份相同)。这里只是按年份排序:
class League < ActiveRecord::Base
def self.default_scope
order(:year)
end
# The season's that are allowed to be used
# This is also the order I'd like to use
def self.season_collection
{
"Spring" => "Spring",
"Summer" => "Summer",
"Fall" => "Fall"
}
end
end
如果我尝试order(:year, :season),那么它将按字母顺序进行。有没有办法使用order(所以在数据库端完成)?
【问题讨论】:
-
数据库中的季节是什么类型?细绳?你想尊重你在 season_collection 中定义的散列顺序吗?我的意思是,先是春天,然后是夏天..
-
是的,抱歉,不清楚。季节是一个字符串,我希望它们按那个顺序排列(“Spring”、“Summer”、“Fall”)
标签: ruby-on-rails activerecord ruby-on-rails-4