2014-04-08 15 views
3

我有以下模型:Rails 4模型是有效的,但不會保存?

通知
class Notification < ActiveRecord::Base 
    belongs_to :notification_path 
    before_save :set_default 

    def set_default 
     self.resolved = false unless self.resolved 
    end 
end 

notification_path
class NotificationPath < ActiveRecord::Base 
    has_many :notifications 
end 

然後,代碼:

通知= Notification.new({ 「區域」 >「test」,「severity」=>「10」,「mess年齡 「=>」 測試」, 「notification_path_id」=> 3})

<Notification id: nil, area: "test", severity: 
10, message: "Test", created_at: nil, updated_at: nil, 
notification_path_id: 3, resolved: nil> 

notification.valid?

真正

notification.errors.full_messages

[]

所以,你可以看到 - 通知是有效的,沒有錯誤。通知模型上沒有驗證。但是,當我保存模型:

notification.save

它不會保存。這是什麼造成的?可能值得注意的是,set_default確實成功運行,並且即使該模型未保存,resolved屬性在嘗試保存時也被設置爲false。

編輯

低於完整的錯誤跟蹤,按照IRB以上時:

notification.save! ActiveRecord :: RecordNotSaved:ActiveRecord :: RecordNotSaved從 /Users/Jonathan/.rvm/gems/[email protected]_notify/gems/activerecord-4.1.0.rc1/lib/active_record/persistence.rb:125:in save!' from /Users/Jonathan/.rvm/gems/[email protected]_notify/gems/activerecord-4.1.0.rc1/lib/active_record/validations.rb:57:in 保存!'從 /Users/Jonathan/.rvm/gems/[email protected]_notify/gems/activerecord-4.1.0.rc1/lib/active_record/attribute_methods/dirty.rb:29:in save!' from /Users/Jonathan/.rvm/gems/[email protected]_notify/gems/activerecord-4.1.0.rc1/lib/active_record/transactions.rb:273:in block in save!'從 /Users/Jonathan/.rvm/gems/[email protected]_notify/gems/activerecord-4.1.0.rc1/lib/active_record/transactions.rb:329:in block in with_transaction_returning_status' from /Users/Jonathan/.rvm/gems/[email protected]_notify/gems/activerecord-4.1.0.rc1/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in 交易中的塊'從 /用戶/Jonathan/.rvm/gems/[email protected]_notify/gems/activerecord-4.1.0.rc1/lib/active_record/connection_adapters/abstract/database_statements.rb:219:在 within_new_transaction' from /Users/Jonathan/.rvm/gems/[email protected]_notify/gems/activerecord-4.1.0.rc1/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in 交易'從 /用戶/Jonathan/.rvm/gems/ruby-2.1。[email protected]_notify/gems/activerecord-4.1.0.rc1/lib/active_record/transactions.rb:208: transaction' from /Users/Jonathan/.rvm/gems/[email protected]_notify/gems/activerecord-4.1.0.rc1/lib/active_record/transactions.rb:326:in with_transaction_returning_status'from /Users/Jonathan/.rvm/gems/[email protected]_notify/gems /activerecord-4.1.0.rc1/lib/active_record/transactions.rb:273:in save!' from (irb):23 from /Users/Jonathan/.rvm/gems/[email protected]_notify/gems/railties-4.1.0.rc1/lib/rails/commands/console.rb:90:in start'from /Users/Jonathan/.rvm/gems/[email protected]_notify/gems/railties-4.1。 0.rc1/lib/rails/commands/console.rb:9:in start' from /Users/Jonathan/.rvm/gems/[email protected]_notify/gems/railties-4.1.0.rc1/lib/rails/commands/commands_tasks.rb:69:in console'from /Users/Jonathan/.rvm/gems/[email protected]_notify/gems/railties-4.1.0.rc1 /lib/rails/commands/commands_tasks.rb:40:in run_command!' from /Users/Jonathan/.rvm/gems/[email protected]_notify/gems/railties-4.1.0.rc1/lib/rails/commands.rb:17:in'from /Users/Jonathan/.rvm/gems/[email protected]_notify/gems/activesupport-4.1.0.rc1/lib/active_support /dependencies.rb:247 : require' from /Users/Jonathan/.rvm/gems/[email protected]_notify/gems/activesupport-4.1.0.rc1/lib/active_support/dependencies.rb:247:in block in require'from /Users/Jonathan/.rvm/gems/[email protected]_notify/gems/activesupport-4.1.0.rc1/lib/active_support/dependencies.rb:232:in load_dependency' from /Users/Jonathan/.rvm/gems/[email protected]_notify/gems/activesupport-4.1.0.rc1/lib/active_support/dependencies.rb:247:in 要求'從/ Users/Jonathan/steel_notify/bin/rails:8:在<top (required)>' from /Users/Jonathan/.rvm/gems/[email protected]_notify/gems/activesupport-4.1.0.rc1/lib/active_support/dependencies.rb:241:in 加載'從 /Users/Jonathan/.rvm/gems/[email protected]_notify/gems/activesupport-4.1.0.rc1 /lib/active_support/dependencies.rb:241:in block in load' from /Users/Jonathan/.rvm/gems/[email protected]_notify/gems/activesupport-4.1.0.rc1/lib/active_support/dependencies.rb:232:in load_dependency'from /Users/Jonathan/.rvm/gems/[email protected]_notify/gems/activesupport-4.1.0.rc1/lib/active_support/ dependencies.rb:241: load' from /Users/Jonathan/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in 要求'from /Users/Jonathan/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb :55:在 require' from -e:1:in'

+0

檢查的最快方法是將'save'更改爲'save!'並查看異常情況。 –

+0

@LoganSerman感謝您的提示 - 我在上面粘貼了我的錯誤,但沒有任何跳出來給我。 – Luigi

回答

10

在Ruby,作業返回被分配的值。在你的情況下,你將self.resolved設置爲false。當您從before_ *回調中精確返回false時,它將取消保存操作。完成作業後,您需要返回不是false的內容。

+0

自從最近30分鐘以來我的腦袋一片汪汪。救了我。謝謝:) –

+0

令人驚歎!謝謝。設置has_many關係時,我努力解決這個問題。我'franchise.businesses << business',但是當我重新加載商業或特許經營時,這種關係就消失了。事實證明,當你推入像這樣的集合時,它實際上執行SQL更新,並且因爲我的業務中有一個'before_save'方法返回false,所以更新被取消。 –

相關問題