Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

There was a problem

You must be a member of this account.

This project is archived and is in readonly mode.

model.errors.empty? broken

#4356

In the rails 2.3 docs, we can see in the ActiveRecord::Errors documentation :

empty?()
Returns true if no errors have been added.

Which is quite nice to have, but now, there's a regression, with rails 3 :

> model.errors
=> #<OrderedHash {}>
> model.errors.empty?
=> true
> model.errors.on :id
=> nil
> model.errors 
=> #<OrderedHash {:id=>[]}>
> model.errors.empty?
=> false

It's all right until I call errors.on something, then the hash is not empty any more, and empty? does not do the right thing.

Reported by Mathieu Arnold · April 9th, 2010 @ 12:05 PM

State: resolved
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Ryan Bigg
    Ryan Bigg

    I can only duplicate this by having an invalid object first and then checking the errors:

    irb(main):002:0> board = Board.new
    => # irb(main):003:0> board.save
    => false irb(main):004:0> board.errors.empty?
    => false irb(main):005:0> board.errors
    => {:title=>["can't be blank"]} irb(main):006:0> board.title = "Some title"
    => "Some title" irb(main):007:0> board.errors.empty?
    => false irb(main):008:0> board.save
    => true irb(main):009:0> board.errors.empty?
    => true

    Other than that, this is behaving as expected.

    April 10th, 2010 @ 08:06 AM

  2. Ryan Bigg
    Ryan Bigg

    Bah, sorry, I made up what your example was doing and I just tried the real-world one. I see now.

    April 10th, 2010 @ 08:07 AM

  3. Ryan Bigg
  4. Repository
    Repository
    • State changed from new to resolved

    (from [d748cc3cd07c6931e5ebc5242f268f1f7ec3d906]) Re-define empty? for errors to check if the values inside the OrderedHash are empty rather than the OrderedHash itself. [#4356 model.errors.empty? broken state:resolved]

    Signed-off-by: José Valim jose.valim@gmail.com
    http://github.com/rails/rails/commit/d748cc3cd07c6931e5ebc5242f268f...

    April 10th, 2010 @ 12:21 PM