2013-10-10 25 views
1

我越來越不能轉儲匿名類錯誤。無法轉儲匿名類#<模塊 - 錯誤緩存在rails 3中

我在我的模型中有以下方法。

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 

我想緩存ln。我收到一個錯誤。無法弄清楚是什麼問題。

can't dump anonymous class #<Module:0x00000007ab6088> 
/home/#/.rvm/gems/[email protected]/gems/activesupport-3.2.14/lib/active_support/cache.rb:561:in `dump' 
/home/#/.rvm/gems/[email protected]/gems/activesupport-3.2.14/lib/active_support/cache.rb:561:in `initialize' 
/home/#/.rvm/gems/[email protected]/gems/redis-activesupport-3.2.4/lib/active_support/cache/redis_store.rb:34:in `new' 
/home/#/.rvm/gems/[email protected]/gems/redis-activesupport-3.2.4/lib/active_support/cache/redis_store.rb:34:in `block in write' 
/home/#/.rvm/gems/[email protected]/gems/activesupport-3.2.14/lib/active_support/cache.rb:520:in `instrument' 
/home/#/.rvm/gems/[email protected]/gems/redis-activesupport-3.2.4/lib/active_support/cache/redis_store.rb:33:in `write' 
/home/#/.rvm/gems/[email protected]/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 

需要指導?

+0

你見過? http://stackoverflow.com/a/14032855/832759 – j03w

+0

@ j03w-ya.i看到了。但我無法找到我犯了什麼錯誤?你可以說得更詳細點嗎?? – Sam

+0

看看http://stackoverflow.com/a/11221230/687142它解釋瞭如何永遠不應該緩存[ActivreRecord :: Relation' – xlembouras

回答

12

問題是你試圖緩存一個ActiveRecord關係集合。這是不可能的。如果你想緩存查詢,你需要首先將它作爲一個數組。

def seating_for(active) 
    cache(col("lu", active)) do 
    self.seat_list_for(active).where(seat_id: self.seat_entries).to_a 
    end 
end 
+0

爲我節省了幾個小時這個.. – Cheyne

相關問題