-1

I have the following model with two named scopes:

class Blog < ApplicationRecord
 scope :past_two_years, -> {where('end_date > ?', Date.today - 2.years)}
 scope :order_by_end_date, -> {order('end_date DESC')}   
end

I now want to define an 'aggregate' scope called past_two_years_and_order_by_end_date. This scope should recycle the existing scopes in order to keep the code DRY.

I know I could create a class method like so:

def self.past_two_years_and_order_by_end_date
  past_two_years.order_by_end_date
end

but I would prefer to do it with a scope as opposed to a class method.

Question: Is this possible? Is it possible to write an aggregate scope that combines/recycles existing scopes?

I did notice this similar question, but it is eight years old and rails syntax has since moved on.

Community
  • 1
  • 1
Neil
  • 4,578
  • 14
  • 70
  • 155

1 Answers1

1

This appears to work:

scope :past_two_years_and_order_by_end_date, -> {self.past_two_years.order_by_end_date}
Neil
  • 4,578
  • 14
  • 70
  • 155