我的应用程序中有一辆模型车.我添加了色域.我的迁移看起来像这样:
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服务器,您的验证将完美无缺.