轨得到验证失败错误,但在ActiveRecord的误差模型没有错误 [英] Rails getting validation failed error, but no errors in ActiveRecord error model

查看:143
本文介绍了轨得到验证失败错误,但在ActiveRecord的误差模型没有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的保存保存模型时具有验证错误的问题! ActiveRecord的误差模型错误信息是空白的,所以我不知道是什么错误都发生在验证尝试。当我按照文档尝试errors.full_messages或errors.each_full,它应该显示的错误,它不

I am having a problem with validation errors when saving a model using save!. The ActiveRecord error model error messages are blank, so i dont know what errors are happening on a validation attempt. When I try errors.full_messages or errors.each_full according to the documentation, it should display the errors, which it doesn't.

的模式,我想保存为订单模式(使用S preE的电子商务网站)。当订单一个项目被删除,update_totals!被称为其重新计算总和,然后保存!被调用,从而触发验证错误(此错误很少发生,但只有当我登录,然后我还没有能够找到它的原因)。订单模型有两个验证其型号为:

The model I am trying to save is the Orders model (ecommerce site using Spree). When an item in the order gets deleted, update_totals! gets called which recalculates the totals, and then save! is called, which triggers the validation error (this error happens very rarely but only when I'm logged in, and I havent been able to find the cause of it). The order model has two validations in its model:

  validates_numericality_of :item_total
  validates_numericality_of :total

我记录order.item_total.inspect,order.total.inspect和order.errors.full_messages.inspect并得到了这一点:

i recorded order.item_total.inspect, order.total.inspect, and order.errors.full_messages.inspect and got this:

Wed Jan 25 08:53:08 -0800 2012order item total: #<BigDecimal:15780c60,'0.279E2',8(16)>
Wed Jan 25 08:53:08 -0800 2012order total: #<BigDecimal:152bf410,'0.2448225E2',12(20)>
Wed Jan 25 08:53:08 -0800 2012: ERRORS SAVING ORDER: 
Wed Jan 25 08:53:08 -0800 2012[]

item_total和总存储在MySQL数据库为十进制(8,2)。最后一行是order.errors.full_messages.inspect,这是一个空数组。验证错误看起来是这样的:

item_total and total are stored in the mySQL database as decimal(8,2). The last line is order.errors.full_messages.inspect, which is an empty array. The validation error looks like this:

ActiveRecord::RecordInvalid (Validation failed: {{errors}}):
  vendor/extensions/mgx_core/app/models/order.rb:382:in `update_totals!'
  vendor/extensions/mgx_core/app/controllers/line_items_controller.rb:7:in `destroy'
  app/middleware/flash_session_cookie_middleware.rb:19:in `call'
  C:\Users\mgx\My Documents\Aptana Studio 3 Workspace\catalogue-spree\script\server:3
  c:/Ruby187/lib/ruby/gems/1.8/gems/ruby-debug-ide-0.4.16/lib/ruby-debug-ide.rb:112:in `debug_load'
  c:/Ruby187/lib/ruby/gems/1.8/gems/ruby-debug-ide-0.4.16/lib/ruby-debug-ide.rb:112:in `debug_program'
  c:/Ruby187/lib/ruby/gems/1.8/gems/ruby-debug-ide-0.4.16/bin/rdebug-ide:87
  c:/Ruby187/bin/rdebug-ide:19:in `load'
  c:/Ruby187/bin/rdebug-ide:19

我想我的问题是双重的:

1。为什么不说我的ActiveRecord的误差模型验证错误是什么?

2。我该如何解决这个问题?难道我的item_total和总有效保存为十进制(8,2)?

我使用的铁轨2.3.5和s preE 0.10.2

I am using rails 2.3.5 and spree 0.10.2

推荐答案

当你有 before_validation 的声明,如果他们返回然后你会得到一个验证失败(的ActiveRecord :: RecordInvalid)消息与空的错误信息(如果没有其他错误)。

When you have before_validation declarations and if they return false then you'll get a Validation failed (ActiveRecord::RecordInvalid) message with an empty error message (if there are no other errors).

注意 before_validation 回调不能返回是好的),这可以通过意外,如发生,如果您正在分配来在回调方法里面的最后一行的布尔属性。明确写入返回true 在回调的方法的,使这项工作(或只是的结束时,如果你的回调是的的(如在评论中指出杰西Wolgamott))。

Note that before_validation callbacks must not return false (nil is okay) and this can happen by accident, e.g., if you are assigning false to a boolean attribute in the last line inside that callback method. Explicitly write return true in your callback methods to make this work (or just true at the end if your callback is a block (as noted by Jesse Wolgamott in the comments)).

这篇关于轨得到验证失败错误,但在ActiveRecord的误差模型没有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆