加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 04:01:42 所属栏目:百科 来源:网络整理
导读:我有以下型号: 通知 class Notification ActiveRecord::Base belongs_to :notification_path before_save :set_default def set_default self.resolved = false unless self.resolved endend notification_path class NotificationPath ActiveRecord::Base
我有以下型号:

通知

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?

true

notification.errors.full_messages

[]

因此,正如您所看到的 – 通知有效且没有错误.通知模型上的验证为零.但是,当我保存模型时:

notification.save

false

它不会保存.是什么造成的?值得注意的是set_default确实运行成功,即使模型未保存,解析的属性在尝试保存时也会设置为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_ *回调中返回完全错误时,它将取消保存操作.在完成作业后,您需要返回一些非虚假的内容.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读