我有以下模型: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:insave!' 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:inblock 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:insave!' 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:instart' 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:inrun_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:inload_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:inblock 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
'
檢查的最快方法是將'save'更改爲'save!'並查看異常情況。 –
@LoganSerman感謝您的提示 - 我在上面粘貼了我的錯誤,但沒有任何跳出來給我。 – Luigi