[英]How to transform sort_by with conditions into a rails scope
我正试图改变self.rank:
def self.rank(courses)
courses.sort_by do |course|
[course.list_order ? course.list_order : Float::INFINITY,
course.upcoming? ? 0 : 1,
course.title
]
end
end
def upcoming?
start_date && start_date > Time.now.utc
end
进入范围,因为self.rank(courses)返回一个数组而不是一个activerecord范围。
当前进度(将更新直到完成):
范围:排名, - > {order(:list_order)}
可能的资源:
scope :just_what_i_want, lambda { |course_id| where(:course_id => course_id) }
scope :ranked, lambda { |list_order| order(list_order ? list_order : Float::INFINITY).order('start_date DESC').order(:title) }
使用如下:
Courses.just_what_i_want([1,2,3,4,5,6]).ranked('course_id')
我认为这应该有效吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.