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 = Notification.new({“area”=>“test”,“severity”=>“10”,“message”=>“Test”,“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 from /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activerecord-4.1.0.rc1/lib/active_record/persistence.rb:125:in save!' from /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activerecord-4.1.0.rc1/lib/active_record/validations.rb:57:in
save!' from /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activerecord-4.1.0.rc1/lib/active_record/validations.rb:57:in
save!' from /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activerecord-4.1.0.rc1/lib/active_record/attribute_methods/dirty.rb:29:in save!' from /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activerecord-4.1.0.rc1/lib/active_record/transactions.rb:273:in
save!' from /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activerecord-4.1.0.rc1/lib/active_record/transactions.rb:273:in
在保存中阻止!' from /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_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/ruby-2.1.1@steel_notify/gems/activerecord-4.1.0.rc1/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in
从/Users/Jonathan/.rvm /gems/ruby-2.1.1@steel_notify/gems/activerecord-4.1.0.rc1/lib/active_record/connection_adapters/abstract/database_statements.rb:219:in within_new_transaction' from /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activerecord-4.1.0.rc1/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in
transaction'from /Users/Jonathan/.rvm/gems/ruby-2.1.1 @ steel_notify / gems / activerecord-4.1.0.rc1 / lib / active_record / transactions.rb:208:in transaction' from /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activerecord-4.1.0.rc1/lib/active_record/transactions.rb:326:in
with_transaction_returning_statu s'from /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activerecord-4.1.0.rc1/lib/active_record/transactions.rb:273:in save!' from (irb):23 from /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/railties-4.1.0.rc1/lib/rails/commands/console.rb:90:in
save!' from (irb):23 from /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/railties-4.1.0.rc1/lib/rails/commands/console.rb:90:in
start' from /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/railties-4.1.0.rc1/lib/rails/commands/console.rb:9:in start'from start' from /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/railties-4.1.0.rc1/lib/rails/commands/commands_tasks.rb:69:in
console'from /Users/Jonathan/.rvm/gems/ruby -2.1.1@steel_notify/gems/railties-4.1.0.rc1/lib/rails/commands/commands_tasks.rb:40:in run_command!' from /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/railties-4.1.0.rc1/lib/rails/commands.rb:17:in
run_command!' from /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/railties-4.1.0.rc1/lib/rails/commands.rb:17:in
/Users/Jonathan/.rvm/ gems/ruby-2.1.1@steel_notify/gems/activesupport-4.1.0.rc1/lib/active_support/dependencies.rb:247:in require' from /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activesupport-4.1.0.rc1/lib/active_support/dependencies.rb:247:in
从/Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activesupport- 4.1.0.rc1 / lib / active_support / dependencies.rb:232:in load_dependency' from /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activesupport-4.1.0.rc1/lib/active_support/dependencies.rb:247:in
/Users/Jonathan/.rvm/gems/ruby-2.1.1@open-support/dependents.rb:247:in'require'from / Users / Jonathan / steel_notify / bin / rails:8:in <top (required)>' from /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activesupport-4.1.0.rc1/lib/active_support/dependencies.rb:241:in
load'from /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activesupport-4.1。 0.rc1 / lib / active_support / dependencies.rb:241:in block in load' from /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activesupport-4.1.0.rc1/lib/active_support/dependencies.rb:232:in
从/ Users / Jonathan / block in load' from /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activesupport-4.1.0.rc1/lib/active_support/dependencies.rb:232:in
load_dependency' .rvm / gems / ruby-2.1.1 @ steel_notify / gems / activesupport-4.1.0.rc1 / lib / active_support / dependencies.rb:241:in load' from /Users/Jonathan/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in
in'from /Users/Jonathan/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems /core_ext/kernel_require.rb:55:in require'from require' from -e:1:in
'
在Ruby中,赋值返回正在分配的值。 在你的情况下,你正在设置self.resolved
为false
。 当您从before_ *回调中返回完全false
,它将取消保存操作。 你需要在完成任务后返回一些不是false
东西。
上一篇: Rails 4 model is valid, but won't save?
下一篇: getting ActiveRecord errors when using mongoid and Sql on Ironworker files