1

我无法保存 Rails 模型,它显示“ActiveRecord::RecordNotSaved”。提前谢谢。

m = Mentor.last

m.save
=> false

m.save!
ActiveRecord::RecordNotSaved

m.valid?
=> true

m.errors.any?
=> false

错误跟踪是:

 from /home/charizard/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.2/lib/active_record/persistence.rb:104:in `save!'
    from /home/charizard/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.2/lib/active_record/validations.rb:56:in `save!'
    from /home/charizard/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.2/lib/active_record/attribute_methods/dirty.rb:33:in `save!'
    from /home/charizard/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.2/lib/active_record/transactions.rb:246:in `block in save!'
    from /home/charizard/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.2/lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
    from /home/charizard/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
    from /home/charizard/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.2/lib/active_record/transactions.rb:208:in `transaction'
    from /home/charizard/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.2/lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
    from /home/charizard/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.2/lib/active_record/transactions.rb:246:in `save!'
    from (irb):120
    from /home/charizard/.rvm/gems/ruby-1.9.3-p429/gems/railties-3.2.2/lib/rails/commands/console.rb:47:in `start'
    from /home/charizard/.rvm/gems/ruby-1.9.3-p429/gems/railties-3.2.2/lib/rails/commands/console.rb:8:in `start'
    from /home/charizard/.rvm/gems/ruby-1.9.3-p429/gems/railties-3.2.2/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
4

1 回答 1

4

我发现了这些家伙。我正在使用 MentorObserver 进行 before_save 回调,该回调返回 false。

如果任何回调返回 false,则保存将不会继续并会引发“ActiveRecord::RecordNotSaved”异常。

我通过使用 git bisect 工具发现了这一点。一个很棒的工具。

于 2013-08-14T13:04:17.343 回答