ruby-on-rails – 错误:当前事务被中止,命令被忽略,直到事务块结束,Ruby on Rails

我的应用程序中有一辆模型车.我添加了色域.我的迁移看起来像这样:

class AddColorToCars < ActiveRecord::Migration
  def change
    add_column :cars,:color,:string
    Car.all.each do |car|
      car.color = "silver"
      car.save
    end
  end
end

以我的形式我添加:

= f.input :color

在汽车模型中,我添加了验证:

validates :color,presence: true

当我尝试编辑现有的Car并将其颜色更改为nil时出现以下错误:

ERROR: current transaction is aborted,commands ignored until end of transaction block

当我禁用我的验证时,一切正常.怎么了?

解决方法

这与PostgreSQL的Rails中的 transactions有关.

On some database systems,such as PostgreSQL,database errors inside a
transaction cause the entire transaction to become unusable until it’s
restarted from the beginning.

您有一个被捕获的事务中止.在ActiveRecord模型上添加验证时有时会导致这种情况.出于某种原因,一旦打破了PostgreSQL的INSERT,就无法在事务中的表上创建任何内容.此外,添加验证有时会破坏INSERT.

快速解决方案重新启动您的Rails服务器,您的验证将完美无缺.

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...