Search code examples
ruby-on-railsrubyactiverecordactiverecord-relation

Specify currently grabbed records within Model class method


I have a class method where I want to modify the records that are currently grabbed by an ActiveRecord::Relation object. But I don't know how to refer to the current scope in a class method. self does not do it.

Example:

class User < ActiveRecord::Base
  ...

  def self.modify_those_records
    #thought implicitly #to_a would be called on currently grabbed records but doesn't work
    temp_users_to_a = to_a
    ...
  end
end

I would use it like this:

User.some_scope.modify_those_records

So User.some_scope would return to me an ActiveRecord::Relation that contains a bunch of User records. I then want to modify those records within that class method and then return them.

Problem is: I don't know how to explicitly refer to "that group of records" within a class method.


Solution

  • You can use current_scope:

    def self.modify_those_records
      current_scope.each do |user|
        user.do_something!
      end
    end
    

    If you want to order Users based on their admin rights, you would be better to use ActiveRecord:

    scope :order_admins_first, order('CASE WHEN is_admin = true THEN 0 ELSE 1 END, id')
    User.some_scope.order_admins_first
    

    This code implies that you have a boolean column is_admin on the users table.