On Jul 12, 10:03 pm, Phoenix Rising <[email protected]> wrote:

> As you can see, instantiating a normal Array object and then calling
> the = operator *replaces* the contents of the array with the new
> array, a pretty standard and expected pattern.  But doing the same
> thing on errors[:base] of an ActiveRecord object *appends* to the
> array, even though it's class is Array.
>
> What could I be doing wrong here?  Or is this just a feature of Rails
> 3?  Should I submit a ticket via Lighthouse?  Again, I'm fully willing
> to accept that I f'd up here. so feel free to tell me what I'm doing
> wrong.  Thanks.

It is a feature. The thing that you are missing is that you're not
using = on an array[1], you're calling the  []= method on the errors
object which has overridden []= to append stuff. A more typical usage
with validations is that various errors are added to an attribute (or
to base) as time goes on. You can see what's being done at
http://github.com/rails/rails/blob/master/activemodel/lib/active_model/errors.rb
which reveals that the original []= method is aliased as set

Fred

[1] Being nitpicky, = is one of the operators in ruby that is not
implemented via a method so writing "calling it" isn't quite write.
Also writing x= ["test"] does not replace the contents of an array -
it just points the local variable x at a new object.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to