Getting error Can't Modify Frozen Object while using memcached in Rails?

You are caching sorting options, not any actual results. When you perform the query, Rails will modify your query options, which won't work, because something from cache is considered immutable.

Up vote 0 down vote favorite share g+ share fb share tw.

I'm having a problem with memcached. I'm using the example by Gregg Pollack here railslab.newrelic.com/2009/02/19/episode... post. Rb def self.

Recent Rails.cache. Fetch('recent_posts', :expires_in => 15. Minutes) do self.

Order('id DESC'). Limit(15) end end In my controller I call it with @posts = Post. Recent and it's written to the cache.

If I reload the page I get the error TypeError: can't modify frozen object. I tried the same thing in the console and I get the same error. The first time I execute @posts = Post.

Recent, the key recent_posts is added to the cache. When I execute the same command the second time, I get the frozen error. I'm using Rails 3.0.1 and working in development mode.

Am I doing something wrong here? Thanks! Tim ruby-on-rails caching ruby-on-rails-3 memcached link|improve this question asked Dec 3 '10 at 3:58Tim21117 85% accept rate.

You are caching sorting options, not any actual results. When you perform the query, Rails will modify your query options, which won't work, because something from cache is considered immutable. Try this: def self.

Recent Rails.cache. Fetch('recent_posts', :expires_in => 15. Minutes) do self.

Order("id DESC"). Limit(15). All end end Now you'll store an array of posts, which you can later use.

This works like a charm. Lesson learned. Thanks!

– Tim Dec 3 '10 at 18:23.

Note that if you're doing this in Rails 3 that because Rails now lazy loads, arel is being used to fetch the records. So all you're storing is technically an active record relation. So you need to address this issue before storing in memcached a la the .

To_a method on the arel. This will make it an array before storing the object. I.e.

@posts = Post.recent. To_a Hope this helps someone.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions