Glenn,

This is a consequence of 1) the "errors=" method accepting a string but
really just appending it's value (and exposing it via "errors") as an array,
and 2) a += b in Ruby being just pretty syntax for a = a + b. With that in
mind, here's what's happening:

> p1.errors[:base] = "p1 error"   # p1.errors[:base] => "['p1 error']"
> p2.errors[:base] = "p2 error"   # p2.errors[:base] => "['p2 error']"

> p2.errors[:base] += p1.errors[:base]  # Same as: p2.errors[:base] =
p2.errors[:base] + p1.errors[:base]
# reduced: p1.errors[:base] = ["p2 error"] + ["p1 error"]
# more:    p1.errors[:base] = ["p2 error", "p1 error"]

And since calling errors= merely appends it to the internal array, you get:
["p2 error", ["p2 error", "p1 error"]]

Hope that helps explain what's going on, even if it I don't necessarily
agree with the ActiveModel implementation of those methods.

- Ben



On Wed, Aug 24, 2011 at 2:56 PM, Glenn Little <[email protected]> wrote:

> What's going on here:
>
> > p1 = Person.new
> > p2 = Person.new
>
> > p1.errors[:base] = "p1 error"
> > p2.errors[:base] = "p2 error"
>
> > p2.errors[:base] += p1.errors[:base]
> > p2.errors
>  => {:base=>["p2 error", ["p2 error", "p1 error"]]}
>
> Any ideas why I'm getting that extra array wrapper, as if I had used
> "<<"?  Not to mention the duplication of the p2 error?  Am I missing
> something dumb?
>
> Thanks for any insight...
>
> -glenn
>
> --
> SD Ruby mailing list
> [email protected]
> http://groups.google.com/group/sdruby
>

-- 
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby

Reply via email to