Nicolo77
Nicolo77

Reputation: 1875

ruby on rails 3 reload using cached data

Using the activerecord reload command in my app seems to be using cached data when called.

I can replicate within the debugger by doing the following.

u = User.find(1)
u.first_name
#outputs bob

# manually change first_name for record 1 to jim with PGadmin or with rails console

u.reload
u.valid?
#outputs true
u.first_name
#outputs bob

#if i do this again
u = User.find(1)
#old data again
u.first_name
#outputs bob

#if i load data this way
u = User.where('id = 1').first
#new data
u.first_name 
#outputs jim

When looking at my log file after running reload it prints out

[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE ("users"."id" = 1) LIMIT 1

So it seems to be using the cache

My environment: Rails 3.0.3, Ruby 1.8.7, Ubuntu 10.04, PostgreSQL 8.4

Upvotes: 3

Views: 2989

Answers (2)

pwnall
pwnall

Reputation: 7134

You need to do a write (e.g. by calling save on some model), so that the query cache gets blown up.

If you look at your development.log when you do reload, ActiveRecord does issue a SELECT query. However, it doesn't blow up the query cache, and it is used to answer the User.find(1) query.

I think that reload is meant for when you update the database from some other part of your application. I also think that you can wrap your find like this to avoid the cache:

ActiveRecord::Base.uncached do
  User.find(1)
end

Upvotes: 8

megas
megas

Reputation: 21791

Make sure that your administrative tool really changed the data in DB. For example write the data by one tool and read this data by another tool.

Upvotes: 0

Related Questions