Shou
(Shou)
1
Hi all
I used rails 3.0.7 and found ActiveRecord::Errors# acts little bit
strange (at least for me).
Let's say we have a model like below.
class User < ActiveRecord::Base
validates :name, :presence => true
end
> u = User.new
> u.valid?
> u.errors #=> { :name => ["can not be nil"] }
> u.errors[:age] # =>
> u.errors #=> { :name => ["can not be nil"], :age => }
Thought ActiveRecord::Errors# looks like just an getter, it actually
rewrites itself.
Is this a correct behavior?
Regards
Shouichi
Thought ActiveRecord::Errors# looks like just an getter, it actually
rewrites itself.
Is this a correct behavior?
While it's a little strange the semantics are that it returns an array
which is empty if there's no error. The implementation is something
like this:
ree-1.8.7-2011.03 :003 > h = Hash.new {|h, k| h[k] = }
=> {}
ree-1.8.7-2011.03 :004 > h[:huh]
=>
ree-1.8.7-2011.03 :005 > h
=> {:huh=>}
Which is why you see what you see.
Shou
(Shou)
3
ree-1.8.7-2011.03 :003 > h = Hash.new {|h, k| h[k] = }
=> {}
ree-1.8.7-2011.03 :004 > h[:huh]
=>
ree-1.8.7-2011.03 :005 > h
=> {:huh=>}
I see. It's the behavior of ruby hash and I can't argue with that.
Thanks!