Yes, you can call the scope from an instance method.
I have been able to call a scope from an instance method using the following pattern:
# Model
class Obj
# using numeric ids for this example
# simple scope to return an instance of the record with an id == 1
scope :get_first_record, -> { find(1) }
def call_scope
# I feel using self.class shows the intent of an instance calling its own Class methods, for readability
self.class.get_first_record
end
end
# Obj.count => 100
obj = Obj.create # obj.id => 101
obj.call_scope.id # = 1
Having the search logic buried inside a class method could later call for a refactor to allow that logic to be reused elsewhere like in
Mrdana's answer.
Therefor, we could do that refactor using my example above, the original scope and using Pavan's point, Mrdana's answer can be rewritten as a scope:
class User
scope :in_good_standing, -> { where(:some_val => 'foo', :some_other_val => 'bar' }
scope :users_in_good_standing, -> (users = all) { find(users.map(&:id)).in_good_standing } # same as: User.in_good_standing.where(:id => self.id) when self is the variable
def in_good_standing?
self.class.users_in_good_standing(self).present?
end
end
This keeps the code DRY, loosely coupled, reusable and extendable; while calling the scope from an instance method.