2010-09-14 19 views
3

使用Rails 2.3.2(目前不能很好地升級)使用ruby 1.8.7(2009-06-12 patchlevel 174)[universal -darwin10.0。如果驗證是:on => :update,則在對模型進行驗證時嘗試保存時出現以下錯誤。如果我將驗證更改爲:on => :create並創建新記錄,則看不到該錯誤,並且如果沒有驗證,則保存它並沒有問題。Rails/Active Record FAILSAFE錯誤「用戶無法引用」

Completed in 392ms (View: 10, DB: 296) | 200 OK [http://localhost/barfoos/update] 
/!\ FAILSAFE /!\ Tue Sep 14 16:38:49 -0400 2010 
    Status: 500 Internal Server Error 
    User can't be referred 
    /path/to/project/vendor/rails/activerecord/lib/active_record/session_store.rb:67:in `dump' 
    /path/to/project/vendor/rails/activerecord/lib/active_record/session_store.rb:67:in `marshal' 
    /path/to/project/vendor/rails/activerecord/lib/active_record/session_store.rb:123:in `marshal_data!' 
    /path/to/project/vendor/rails/activesupport/lib/active_support/callbacks.rb:178:in `send' 
    /path/to/project/vendor/rails/activesupport/lib/active_support/callbacks.rb:178:in `evaluate_method' 
    /path/to/project/vendor/rails/activesupport/lib/active_support/callbacks.rb:166:in `call' 
    /path/to/project/vendor/rails/activesupport/lib/active_support/callbacks.rb:93:in `run' 
    /path/to/project/vendor/rails/activesupport/lib/active_support/callbacks.rb:92:in `each' 
    /path/to/project/vendor/rails/activesupport/lib/active_support/callbacks.rb:92:in `send' 
    /path/to/project/vendor/rails/activesupport/lib/active_support/callbacks.rb:92:in `run' 
    /path/to/project/vendor/rails/activesupport/lib/active_support/callbacks.rb:276:in `run_callbacks' 
    /path/to/project/vendor/rails/activerecord/lib/active_record/callbacks.rb:344:in `callback' 
    /path/to/project/vendor/rails/activerecord/lib/active_record/callbacks.rb:249:in `create_or_update' 
    /path/to/project/vendor/rails/activerecord/lib/active_record/base.rb:2539:in `save_without_validation' 
    /path/to/project/vendor/rails/activerecord/lib/active_record/validations.rb:1009:in `save_without_dirty' 
    /path/to/project/vendor/rails/activerecord/lib/active_record/dirty.rb:79:in `save_without_transactions' 
    /path/to/project/vendor/rails/activerecord/lib/active_record/transactions.rb:229:in `send' 
    /path/to/project/vendor/rails/activerecord/lib/active_record/transactions.rb:229:in `with_transaction_returning_status' 
    /path/to/project/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:136:in `transaction' 
    /path/to/project/vendor/rails/activerecord/lib/active_record/transactions.rb:182:in `transaction' 
    /path/to/project/vendor/rails/activerecord/lib/active_record/transactions.rb:228:in `with_transaction_returning_status' 
    /path/to/project/vendor/rails/activerecord/lib/active_record/transactions.rb:196:in `save_without_unsaved_flag' 
    /path/to/project/vendor/rails/activerecord/lib/active_record/transactions.rb:208:in `rollback_active_record_state!' 
    /path/to/project/vendor/rails/activerecord/lib/active_record/transactions.rb:196:in `save_without_unsaved_flag' 
    /path/to/project/vendor/plugins/active_scaffold/lib/extensions/unsaved_record.rb:15:in `save' 
    /path/to/project/vendor/rails/activerecord/lib/active_record/session_store.rb:300:in `set_session' 
    /path/to/project/vendor/rails/activerecord/lib/active_record/base.rb:1453:in `silence' 
    /path/to/project/vendor/rails/activerecord/lib/active_record/session_store.rb:297:in `set_session' 
    /path/to/project/vendor/rails/actionpack/lib/action_controller/session/abstract_store.rb:132:in `call' 
    /path/to/project/vendor/rails/activerecord/lib/active_record/query_cache.rb:29:in `call' 
    /path/to/project/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:34:in `cache' 
    /path/to/project/vendor/rails/activerecord/lib/active_record/query_cache.rb:9:in `cache' 
    /path/to/project/vendor/rails/activerecord/lib/active_record/query_cache.rb:28:in `call' 
    /path/to/project/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:361:in `call' 
    /path/to/project/vendor/rails/actionpack/lib/action_controller/reloader.rb:9:in `call' 
    /path/to/project/vendor/rails/actionpack/lib/action_controller/failsafe.rb:11:in `call' 
    /Library/Ruby/Gems/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:in `call' 
    /Library/Ruby/Gems/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:in `synchronize' 
    /Library/Ruby/Gems/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:in `call' 
    /path/to/project/vendor/rails/actionpack/lib/action_controller/dispatcher.rb:106:in `call' 
    /path/to/project/vendor/rails/railties/lib/rails/rack/static.rb:31:in `call' 
    /Library/Ruby/Gems/1.8/gems/rack-1.0.1/lib/rack/urlmap.rb:46:in `call' 
    /Library/Ruby/Gems/1.8/gems/rack-1.0.1/lib/rack/urlmap.rb:40:in `each' 
    /Library/Ruby/Gems/1.8/gems/rack-1.0.1/lib/rack/urlmap.rb:40:in `call' 
    /path/to/project/vendor/rails/railties/lib/rails/rack/log_tailer.rb:17:in `call' 
    /Library/Ruby/Gems/1.8/gems/rack-1.0.1/lib/rack/content_length.rb:13:in `call' 
    /Library/Ruby/Gems/1.8/gems/rack-1.0.1/lib/rack/handler/webrick.rb:50:in `service' 
    /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/httpserver.rb:104:in `service' 
    /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run' 
    /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread' 
    /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:162:in `start' 
    /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread' 
    /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:95:in `start' 
    /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:92:in `each' 
    /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:92:in `start' 
    /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:23:in `start' 
    /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:82:in `start' 
    /Library/Ruby/Gems/1.8/gems/rack-1.0.1/lib/rack/handler/webrick.rb:14:in `run' 
    /path/to/project/vendor/rails/railties/lib/commands/server.rb:111 
    /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem_original_require' 
    /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require' 
    script/server:3 

這個模型看起來是這樣的:

class Barfoo < ActiveRecord::Base 
    default_scope :conditions => {:scoping_model_id => ScopingModel.current_version.id} 
    belongs_to :scoping_model 

    # validate is false to keep from attempting to validate Foobar on Barfoo save, as Foobar can be saved, even if invalid, unlike Barfoo. 
    has_one :foobar, :validate => false 
    validates_presence_of :foobar_id, :on => :create, :message => "can't be blank" 
    validates_inclusion_of :accepted_an_agreement, :in => [true, false], :on => :update, :message => "please choose whether you agree or disagree" 
    validates_presence_of :some_option_string, :on => :update, :message => "before agreeing, you must specify the some string", :if => Proc.new { |detail| detail.accepted_an_agreement == true } 
    validates_presence_of :some_text, :on => :update, :message => "you must provide details if option is 'Other'", :if => Proc.new { |detail| detail.some_option_string == 'Other' } 
end 

我指定爲這些字段的值並不重要,並且在下面他的評論Shadwell筆記,這似乎是會話相關的。我們在ApplicationController中執行的會話相關和用戶相關的一件事可能會導致此問題,即使它現在從來沒有成爲問題。 ApplicationController的樣子:

class ApplicationController < ActionController::Base 
    include ExceptionNotifiable 
    require 'something_that_defines_update_method_that_we_redefine_in_controller' 
    include Userstamp 

    ActionController::Base.session_options[:httponly] = true 
    ActionController::Base.session_options[:secure] = true 

    prepend_before_filter :user_setting_method 

    ... 

    protected 

    def user_setting_method 
    session[:username] ||= optional_override_username 
    session[:username] = session[:username] # touch the session for timeout purposes 
    @user ||= User.find_by_username session[:username] 
    true 
    end 

    ... 

end 

搜索並沒有帶來太多了約/!\ FAILSAFE /!\User can't be referred。任何關於什麼會導致這種情況的想法,有沒有常見的解決方案來尋找這個或那些可能是錯誤的?

請注意,附加到此的foobar實例已保存,但在此特定情況下,foobar之前很久以前已保存過,但未驗證(保存未經驗證,這是正常的可能狀態,因爲它是數據從部分保存數據的多頁形式,用戶可以返回並在稍後修正完成)。

+0

這似乎不是保存你正在更新的對象的問題,而是似乎是持續存在於會話中的任何問題。作爲此更新操作的一部分,您是否在會話中存儲了某些內容?當驗證失敗時,可能值得發佈導致錯誤的驗證以及您正在使用的參數。 – Shadwell 2010-09-14 22:05:55

+0

剛剛更新了這個信息的問題。謝謝!我最困惑的是爲什麼這個問題現在正在發生,只有:on =>:更新驗證,而不是:on =>:創建驗證。我開始懷疑ApplicationController中的require要比通過一個大的class_eval定義更新方法,儘管這是永遠存在的,我會認爲它會在BarfooController類中重新定義。 – 2010-09-15 19:20:45

回答

1

繼續開發模型後多一點我開始另一個錯誤:

/!\ FAILSAFE /!\ Fri Sep 17 14:53:50 -0400 2010 
    Status: 500 Internal Server Error 
    can't dump hash with default proc 

我試圖保存在控制檯中的模式和成功,但看到了一些奇怪的事情。當我已經在模型上設置了有效的foobar時,它要求foobar_id有效(例如)。所以,我和一個團隊成員談論了這件事,並且聽說他已經閱讀了關於試圖驗證這樣的關聯對象的id的問題。

我刪除:

validates_presence_of :foobar_id, :on => :create, :message => "can't be blank" 

和它的工作。

但是,它是由另一同事,我是有這個問題,很可能涉及到做validates_presence_of :foobar_id的時候,在控制器中,我試圖設置foobar向我建議:

barfoo.foobar = foobar 

,而不是設置foobar_id,如:

barfoor.foobar_id = foobar.id 

我也注意到,與前例子,它沒有在數據庫中的相關行中設置foobar_id

將它更改爲foobar_id設置爲barfoo後,只要沒有更新驗證,它就會保存在創建和更新中。但是,如果我添加驗證:on => :update,它會失敗。

0

我在這裏很晚。但我也有同樣的問題,我得到了一個臨時的解決辦法爲它

只是覆蓋在activerecord/lib/active_record/session_store.rb

ActiveRecord::SessionStore::ClassMethods.module_eval do 
    def marshal(data) 
    # To prevent from Marshal dump error 
    begin  
     ::Base64.encode64(Marshal.dump(data)) if data 
    rescue  
     # Checks for any ActiveRecord object in session variables and reloads it 
     data.each_pair do |key,obj| 
     if obj.is_a?(ActiveRecord::Base) || obj.is_a?(Array) 
      unless obj.is_a?(Array)  
      data[key] = obj.reload    
      next         
      end       
      data[key].collect do |it_obj| 
      if obj.is_a?(ActiveRecord::Base)  
       it_obj.reload        
      else         
       it_obj          
      end         
      end       
     end      
     end    
     ::Base64.encode64(Marshal.dump(data)) if data 
    end   

    end 
end 

名帥方法簡單,它會重新加載所有的ActiveRecord對象存在於會話變量

相關問題