Thank you both Fred and Jordon for the help.
I followed Fred's code example and ended with the following solution:
class Profile < ActiveRecord::Base
has_many :person_profiles
has_many :people, through: :person_profiles
def self.cached
@@profiles ||= fetch_data
end
def self.fetch_data
temp_profiles = {}
Profile.all.each do |profile|
when 'admin'
temp_profiles[:admin] = profile
when 'super-admin'
temp_profiles[:super_admin] = profile
# ... other profiles
end
end
temp_profiles
end
end
In the controller:
@admins = Profile.cached[:admin].people
Doing some tests, I noticed that starting on the second request there is a significative difference in ActiveRecord´s total processing time. However, if all the users log out, it seems that the garbage collector sweeps the data cached in the class variable. When I have time, I will further investigate this in order to find a way to persist that data for all the time the application is running.
Cheers,
Alex.