我有一個按鈕,通過XHR發送獲取請求到特定動作一個導軌服務器。 此操作調用我在模型「類別」中定義的函數。此函數執行類似x = Category.subcategories的操作,並在下一行中提供類似x.map(&:id)或x.each {| x | y < < x.id}。 (類別has_many:子類別)。在我的開發環境中,這適用於1-10點擊此按鈕,但隨後停止,原因如下工作:陣列上的「each」/「map」結果在「create_time_zone_conversion_attribute?」中有「你沒有期望它的零對象」,零星
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.include?
用下面的轉儲:
C:/MyApp/vendor/rails/activerecord/lib/active_record/attribute_methods.rb:142:in `create_time_zone_conversion_attribute?'
C:/MyApp/vendor/rails/activerecord/lib/active_record/attribute_methods.rb:75:in `define_attribute_methods'
C:/MyApp/vendor/rails/activerecord/lib/active_record/attribute_methods.rb:71:in `each'
C:/MyApp/vendor/rails/activerecord/lib/active_record/attribute_methods.rb:71:in `define_attribute_methods'
C:/MyApp/vendor/rails/activerecord/lib/active_record/attribute_methods.rb:355:in `respond_to?'
C:/MyApp/vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:215:in `method_missing'
C:/MyApp/vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:215:in `map'
C:/MyApp/vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:215:in `send'
C:/MyApp/vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:215:in `method_missing'
C:/MyApp/vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:369:in `method_missing'
C:/MyApp/app/models/category.rb:24:in `fetch_prices_grouped_by_date'
C:/MyApp/app/controllers/categories_controller.rb:103:in `show'
重新啓動服務器解決了問題接下來的1-10次點擊,然後重新出現。 (在NetBeans IDE中,ruby-debug-ide-0.3.1),我得到了(在穿過1-10個「良好的點擊」之後)method_missing(method =「map」,self =「{Array ,23個元素}「),如果我嘗試擴展變量x的監視,調試服務器崩潰。 繼this post建議,我編輯environment.rb中,轉身級緩存上:
config.cache_classes = true
,問題就解決了。但是,在每次代碼更改後重新啓動服務器都非常煩人,並且我想知道是否有人知道發生了什麼,爲什麼更改cache_classes可以解決這個問題,並且如果還有其他解決方法無法執行每次編輯後重新開始的痛苦。
我正在使用Rails 2.3.8。
非常感謝, 阿米特
編輯:
OK,所以一些更多的調試我意識到以後失敗在具有設置爲nil出於某種原因在create_time_zone_conversion_attribute skip_time_zone_conversion_for_attributes?方法(active_record/attribute_methods.rb)。
經過一番研究發現this article。 添加:
self.skip_time_zone_conversion_for_attributes = []
模型不起作用。
替換的environment.rb
config.time_zone = 'UTC'
與
config.active_record.default_timezone = :utc
對我也不起作用,因爲我碰到一個 「堆棧層次過深」 與:
C:/MyApp/vendor/rails/activerecord/lib/active_record/attribute_methods.rb:237:in `method_missing'
C:/MyApp/vendor/rails/activerecord/lib/active_record/attribute_methods.rb:253:in `method_missing'
C:/MyApp/vendor/rails/activerecord/lib/active_record/attribute_methods.rb:211:in `to_proc'
C:/MyApp/vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:369:in `method_missing'
C:/MyApp/vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:215:in `map'
C:/MyApp/vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:215:in `send'
C:/MyApp/vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:215:in `method_missing'
C:/MyApp/vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:369:in `method_missing'
C:/MyApp/app/models/category.rb:24:in `fetch_prices_grouped_by_date'
C:/MyApp/app/controllers/categories_controller.rb:103:in `show'
我會進一步調查,看看它是否只是我的代碼的特定問題。
EDIT#2
確定,每this article's建議我編輯attributes_methods。
if self.class.primary_key.to_s == method_name
到:RB線252和改變了它
if false and self.class.primary_key.to_s == method_name
而現在它的工作原理。然而,不知道我喜歡亂搞框架的代碼。 希望有任何其他解決方法的建議。
謝謝! 阿米特
編輯#3
好了,最後這個變化打破了我的應用程序的其他部分。 我已轉貼這個問題here。
當右鍵我有完全相同的問題,和我的關係也被稱爲類別。它可能是一個命名問題嗎?在過去,我有一個問題,爲我的模型命名爲kind。 – Macario 2010-11-10 20:05:56
馬卡里奧,我不認爲這是命名。你有沒有在你的應用程序中的任何地方模型類不是從ActiveRecord :: Base派生?我認爲這是我的問題。我有一個FilterCategory類,它具有「require'category'」,我認爲這弄亂了Rails的類緩存。 – AmitA 2010-11-11 03:17:00