xxxxxxxxxx
class Book < ApplicationRecord
scope :costs_more_than, ->(amount) { where("price > ?", amount) }
end
xxxxxxxxxx
class Movie < ApplicationRecord
scope :newest, -> { order(created_at: :desc) }
scope :this_year, -> { where(year: Time.current.year) }
scope :filter_by_category, ->(category) { where(category: category) }
scope :old_movies, -> { where(year: 30.years.ago.year) }
end
xxxxxxxxxx
class Test < ActiveRecord::Model
scope :complicated, ComplicatedScope
class ComplicatedScope
def self.call(*args) # in new syntax use (...)
new(*args).call
end
def initialize(arg_1) # provide scope arguments
@arg_1 = arg_1
end
def call
module_parent
.joins("complicated JOIN querry")
.where("complicated where ?", @arg_1)
.merge("some merging")
end
delegate :module_parent, to: :class
end
end
Test.complcated("arg_1")