Ответ 1
Вы можете использовать ActiveRecord::QueryCache.uncached
следующим образом:
User.find_by_email('[email protected]')
User.find_by_email('[email protected]') # Will return cached result
User.uncached do
User.find_by_email('[email protected]')
User.find_by_email('[email protected]') # Will query the database again
end
В контроллере он будет выглядеть примерно так:
def show # users#index action
User.uncached do
@user = User.find_by_email('[email protected]')
@another_user = User.find_by_email('[email protected]') # Will query database
end
User.find_by_email('[email protected]') # Will *not* query database, as we're outside of the Users.uncached block
end
Очевидно, что в модели вам просто нужно сделать:
class User < ActiveRecord::Base
def self.do_something
uncached do
self.find_by_email('[email protected]')
self.find_by_email('[email protected]') # Will query database
end
end
end
User.do_something # Will run both queries