按属性对不同对象的ruby排序数组
我有一个具有相同属性的不同对象数组按属性对不同对象的ruby排序数组,ruby,Ruby,我有一个具有相同属性的不同对象数组 class Report1 < ActiveRecord::Base end class Report2< ActiveRecord::Base end class Report3< ActiveRecord::Base end 如何按日期字段对其排序 我试图使用.sort,但出现了一个错误,即这些对象是不同类型的您是否尝试过按排序 @reports_all = @reports_all.sort_by(&:date) 或
class Report1 < ActiveRecord::Base
end
class Report2< ActiveRecord::Base
end
class Report3< ActiveRecord::Base
end
如何按日期
字段对其排序
我试图使用.sort,但出现了一个错误,即这些对象是不同类型的您是否尝试过按排序
@reports_all = @reports_all.sort_by(&:date)
或
你试过按排序吗
@reports_all = @reports_all.sort_by(&:date)
或
尝试此排序方法,该方法在结尾处处理零值:
@reports_all = @reports_all.sort_by{|report| [report.date ? 0 : 1, report.date]}
尝试此排序方法,该方法在结尾处处理零值:
@reports_all = @reports_all.sort_by{|report| [report.date ? 0 : 1, report.date]}
简言之,你做错了。直接在数据库中选择并排序:
@reports_all = ActiveBase::Connection.execute(<<-SQL
(SELECT * FROM report1 WHERE ...
UNION
SELECT * FROM report2 WHERE ...
UNION
SELECT * FROM report3 WHERE ... )
ORDER BY date
SQL
)
@reports\u all=ActiveBase::Connection.execute(简而言之,您做错了。直接在数据库中选择并排序:
@reports_all = ActiveBase::Connection.execute(<<-SQL
(SELECT * FROM report1 WHERE ...
UNION
SELECT * FROM report2 WHERE ...
UNION
SELECT * FROM report3 WHERE ... )
ORDER BY date
SQL
)
@reports\u all=ActiveBase::Connection.execute(`sort\u by':将日期与nil进行比较失败(ArgumentError)确定。您必须选择是将零日期放在数组的开头还是结尾`sort\u by':将日期与nil进行比较失败(ArgumentError)好的,你必须选择在arraylet的最后一句话的开头是零日期还是结尾是零日期