Zyren
Zyren

Reputation: 663

Rails getting validation failed error, but no errors in ActiveRecord error model

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.

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

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 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

I guess my question is twofold:

1. Why is my activerecord errors model not saying what the validation error is?

2. How do I fix this problem? Is my item_total and total valid for saving as decimal(8,2)?

I am using rails 2.3.5 and spree 0.10.2

Upvotes: 18

Views: 13445

Answers (6)

Geesu
Geesu

Reputation: 6018

Throwing a reply in here as it took a bit for us to track this down. We were upgrading to Rails 5.2 and suddenly started getting this exception.

It was due to us overriding destroyed? on the model (we were soft deleting items).

Upvotes: 0

When you create other register in a before_validation method, if it fails, the error will be thrown by the 'father' class, so it won't show error, just <ActiveRecord::RecordInvalid: Validation failed: > I noticed that when I got an error in my 'child' record using byebug inside before validation method

Upvotes: 0

Vikrant Chaudhary
Vikrant Chaudhary

Reputation: 11319

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).

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)).

UPDATE: This will no longer be an issue starting Rails 5.0, as return false will no longer halt the callback chain (throw :abort will now halt the callback chain).

UPDATE: You might also receive ActiveRecord::RecordNotSaved: Failed to save the record if a callback returns false.

Upvotes: 28

ReggieB
ReggieB

Reputation: 8257

It looks to me like you are using Ruby 1.8.7. Have you tried running your app using Ruby 1.9.3?

Upvotes: 0

johnsampson
johnsampson

Reputation: 386

Regarding 1. Why is my activerecord errors model not saying what the validation error is?, see if you have the gem i18n installed. If you do, try uninstalling or an earlier version of the gem i18n.

gem uninstall i18n

Upvotes: 0

Zubin
Zubin

Reputation: 9742

I think the problem lies in the controller code. The order variable is set before the line item is destroyed, and is not aware it's been destroyed afterwards. This code should really be in the model:

# line_item.rb
after_destroy :update_totals!
delegate :update_totals, :to=> :order

And the controller should just destroy the line item.

Upvotes: 0

Related Questions