ruby-on-rails - Rails 4 模型有效,但不会保存?

标签 ruby-on-rails validation model

我有以下型号:

通知

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

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

通知路径

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?

true

notification.errors.full_messages

[]

因此,如您所见 - 通知有效且没有错误。通知模型的验证为零。但是,当我保存模型时:

notification.save

false

它不会保存。这是什么原因造成的?值得注意的是 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/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 block in save!' 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 block in 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: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_status' 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 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 /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' from /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 block in require' 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' from /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/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 load_dependency' 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/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in require' from /Users/Jonathan/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in require' from -e:1:in'

最佳答案

在 Ruby 中,赋值返回被赋值的值。在您的情况下,您将 self.resolved 设置为 false。当您从 before_* 回调中准确返回 false 时,它将取消保存操作。完成分配后,您需要返回不是 false 的内容。

关于ruby-on-rails - Rails 4 模型有效,但不会保存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22926614/

相关文章:

css - rails bootstrap 缺少类

javascript - AngularJS 的面向对象方法

php - 如何在 CakePHP 中为一个模型动态使用多个数据库

ruby-on-rails - Rails 生产中未初始化常量

ruby-on-rails - 建模收藏夹

ruby-on-rails - 安装 rails : nokogiri requires Ruby version < 2. 4 时出错,>= 2.1.0

html - html5 输入类型 url 验证中的模式

如果所有输入均为 true,Javascript 错误代码验证错误边框框将隐藏

Javascript 表单验证 - 只有第一个字符必须是数字

ruby-on-rails - 在 model.save 或 model.save 中捕获数据库异常!方法?