Rails – 按复杂排序排序索引

如何通过featured_end_date >= Time.now():asc order中订购我的索引结果,然后将其余结果按publish_at: :desc排序。

目前我有BlogPost.order(featured_end_date: :asc, publish_at: :desc) 。 我错过了>= Time.now()比较。

我假设可能需要使用范围,但我不确定如何实现这一点。

BlogPost模型

 scope :featuredfuture, -> { where("featured_end_date >= ?", Time.now()).order(featured_end_date: :asc) } scope :other, -> { where("featured_end_date < ? or featured_end_date is null", Time.now()).order(publish_at: :desc) } 

调节器

 @blogposts = BlogPost.featuredfuture + BlogPost.other 

您需要在订购前指定您的collections。 你必须在where使用。

 BlogPost.where('featured_end_date >= ?', Time.now).order(featured_end_date: :asc, publish_at: :desc)