Properly cache value when it is "false" #2010

merged 1 commit into from Jul 8, 2011


None yet

7 participants

hasclass commented Jul 8, 2011

Rails.cache does not properly cache "false", it returns nil instead.

This is because the value is checked with an

if value


value = nil # => sets false values to nil

I changed it to:
if defined?(value)


Rails.cache.fetch('foo') { false }
=> false # uncached value

Rails.cache.fetch('foo') { false }
=> nil # from cache

pzac commented Jul 8, 2011


sikachu commented Jul 8, 2011

Nice catch! 👍

@spastorino spastorino merged commit 4f7ca00 into rails:master Jul 8, 2011

Any chance getting this patch into Rails 3.1?


@jamestyj This will be available in 3.1.2


@vijaydev cherry picking this into 3-1-stable broke the build. the implementation in master has changed since this PR was merged. additionally, the test in this PR actually passes without the fix, so I think even master exhibits this bug still. (also, in the first line of this patch, defined?(value) will always be truthy as it is an argument to the method.)

I am going to revert the cherry-pick for now. I am planning to push 3.1.2rc1 quite soon, but I can wait for a fix for this if you intend to work on it?


@jonleighton This is fixed in master (by using @value = Marshal.dump(value)).


Fixed in cd392fd and passes Travis. Good to go for 3.1.4 :)

Finally, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment