5

I am getting can't dump anonymous class error.

I have the following methods in my model.

 def seating_for(active)
   ln = cache(col("lu", active)) do
    self.seat_list_for(active).where(seat_id: self.seat_entries)
 end

 def seat_list_for(active)
   ex_id = Exc.id_by_symbol(active)
   self.row.exe.seats.where(ex_id: exc_id).first.seat_alloc_mem
 end

I am trying to cache ln. I am getting an error. Can't figure out what is the problem.

 can't dump anonymous class #<Module:0x00000007ab6088>
 /home/#/.rvm/gems/ruby-2.0.0-p0@anon-ui/gems/activesupport-3.2.14/lib/active_support/cache.rb:561:in `dump'
 /home/#/.rvm/gems/ruby-2.0.0-p0@anon-ui/gems/activesupport-3.2.14/lib/active_support/cache.rb:561:in `initialize'
 /home/#/.rvm/gems/ruby-2.0.0-p0@anon-ui/gems/redis-activesupport-3.2.4/lib/active_support/cache/redis_store.rb:34:in `new'
 /home/#/.rvm/gems/ruby-2.0.0-p0@anon-ui/gems/redis-activesupport-3.2.4/lib/active_support/cache/redis_store.rb:34:in `block in write'
 /home/#/.rvm/gems/ruby-2.0.0-p0@anon-ui/gems/activesupport-3.2.14/lib/active_support/cache.rb:520:in `instrument'
 /home/#/.rvm/gems/ruby-2.0.0-p0@anon-ui/gems/redis-activesupport-3.2.4/lib/active_support/cache/redis_store.rb:33:in `write'
 /home/#/.rvm/gems/ruby-2.0.0-p0@anon-ui/gems/activesupport-3.2.14/lib/active_support/cache.rb:299:in `fetch'
 /home/#/user/projects/app/#/lib/lib_utility.rb:15:in `cache'
 /home/#/user/projects/app/#/app/models/smthng.rb:566:in `seating_for'

lib/lib_utility.rb

module LibUtility
  module ClassMethods
    def col(p, l)
     //smthng
    end

   def cache(l, options={}, &b)
     Rails.cache.fetch(l, expires_in: 50.minutes, &b)
   end
  end

  def self.included(receiver)
    receiver.extend ClassMethods
  end

end

Needs guidance??

Sam
  • 5,040
  • 12
  • 43
  • 95
  • have you seen this? http://stackoverflow.com/a/14032855/832759 – j03w Oct 10 '13 at 08:47
  • @j03w- ya.i saw that. but i m unable to find what mistake i ve done?? can you be more specific?? – Sam Oct 10 '13 at 10:45
  • take a look at http://stackoverflow.com/a/11221230/687142 it explains how you should never cache an `ActivreRecord::Relation` – xlembouras Oct 10 '13 at 11:04
  • possible duplicate of [Confusion caching Active Record queries with Rails.cache.fetch](http://stackoverflow.com/questions/11218917/confusion-caching-active-record-queries-with-rails-cache-fetch) – xlembouras Feb 26 '14 at 13:36

1 Answers1

18

The problem is that you try to cache an ActiveRecord relation collection. That is not possible. If you want to cache the query, you need to make it an array first

def seating_for(active)
  cache(col("lu", active)) do
    self.seat_list_for(active).where(seat_id: self.seat_entries).to_a
  end
end
xlembouras
  • 8,215
  • 4
  • 33
  • 42