www.flickr.com
Michael Kovacs' photos More of Michael Kovacs' photos
Recommend Me Cable Car Software logo

Tuesday, November 10, 2009

Rails realities part 28 - Sinatra holdin' up the cache-money

For those of you looking to integrate the cache-money write through caching within your rails app and you're using Sinatra to create some metals beware.

It all started with this command on the rails console:
>> a = User.find(1)

Which resulted in this error message:


NoMethodError: You have a nil object when you didn't expect it!
The error occurred while evaluating nil.key?
from /Library/Ruby/Gems/1.8/gems/sinatra-0.9.4/lib/sinatra/base.rb:768:in 'route'
from /Library/Ruby/Gems/1.8/gems/sinatra-0.9.4/lib/sinatra/base.rb:754:in 'get'
from (__DELEGATE__):2:in 'send'
from (__DELEGATE__):2:in 'get'
from /Library/Ruby/Gems/1.8/gems/nkallen-cache-money-0.2.5/lib/cash/transactional.rb:27:in 'send'
from /Library/Ruby/Gems/1.8/gems/nkallen-cache-money-0.2.5/lib/cash/transactional.rb:27:in 'method_missing'
from /Library/Ruby/Gems/1.8/gems/nkallen-cache-money-0.2.5/lib/cash/accessor.rb:22:in 'fetch'
from /Library/Ruby/Gems/1.8/gems/nkallen-cache-money-0.2.5/lib/cash/accessor.rb:31:in 'get'
from /Library/Ruby/Gems/1.8/gems/nkallen-cache-money-0.2.5/lib/cash/query/abstract.rb:65:in 'hit_or_miss'
from /Library/Ruby/Gems/1.8/gems/nkallen-cache-money-0.2.5/lib/cash/query/abstract.rb:18:in 'perform'
from /Library/Ruby/Gems/1.8/gems/nkallen-cache-money-0.2.5/lib/cash/query/abstract.rb:7:in 'perform'
from /Library/Ruby/Gems/1.8/gems/nkallen-cache-money-0.2.5/lib/cash/finders.rb:24:in 'find_every'


Huh? Uhh, OK... WTF is the code doing in Sinatra? After some investigation I came across this very similar problem from someone using the ruby redis client https://sinatra.lighthouseapp.com/projects/9779/tickets/251-sinatra-collision-with-redis-client. Since that stack trace looked familiar I looked a little more closely. Turns out this bit of documentation is the key:


"The methods available to Sinatra::Base subclasses are exactly as those available via the top-level DSL. Most top-level apps can be converted to Sinatra::Base components with two modifications:

* Your file should require sinatra/base instead of sinatra;
otherwise, all of Sinatra's DSL methods are imported into the main namespace."

Read more here:
http://github.com/sinatra/sinatra/commit/536033334f33c6822acb64e18b6cdb4eee2f85fe#L0R515

Turns out the rails app I was working on which has some metals had included sinatra instead of sinatra/base. Simply changing that require fixed the problem with cache-money. Hopefully this saves someone else that same headache that cost me more time than it should've.

Labels:


This page is powered by Blogger. Isn't yours?