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 irb> u = User.new irb> u.valid? irb> u.errors #=> { :name => ["can not be nil"] } irb> u.errors[:age] # => [] irb> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Michael Koziarski
2011-May-29 08:55 UTC
Re: Is ActiveModel::Errors#[]''s behavior correct?
> 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. -- Cheers Koz -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
> 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! -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.