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
Best Answer
When you have
before_validation
declarations and if they returnfalse
then you'll get aValidation failed (ActiveRecord::RecordInvalid)
message with an empty error message (if there are no other errors).Note that
before_validation
callbacks must not returnfalse
(nil
is okay) and this can happen by accident, e.g., if you are assigningfalse
to a boolean attribute in the last line inside that callback method. Explicitly writereturn true
in your callback methods to make this work (or justtrue
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 returnsfalse
.