Wilson Bilkovich
11/1/2006 4:08:00 PM
On 11/1/06, dblack@wobblini.net <dblack@wobblini.net> wrote:
> Hi --
>
> On Thu, 2 Nov 2006, ara.t.howard@noaa.gov wrote:
>
> > On Thu, 2 Nov 2006 dblack@wobblini.net wrote:
> >
> >> It was recently noted on the Rails mailing list that something like
> >> that happens in ActiveRecord. Given Person and Address models, where
> >> Person has_one Address:
> >>
> >> a = person.address
> >> person.address = Address.find(some_other_address)
> >> puts a # a has changed!
> >>
> >> I find it un-Rubyish and disconcerting (though I'm still waiting for
> >> someone to explain the rationale; there may be something I'm missing).
> >
> > can you show an example? are you saying that activerecord avoids apparent
> > copy-on-write semantics with some trickery?
>
> Here's an example from a program which models Ideas; each Idea belongs
> to a Page.
>
> >> i = Idea.find(6)
> => #<Idea:0xb79eec2c @attributes={"page_id"=>"6", "id"=>"6",
> "content"=>"Go to Paris"}>
>
> # Save the idea's page in p
> >> p = i.page
> => #<Page:0xb79e4a88 @attributes={"scratchpad_id"=>"6",
> "title"=>"Domestic travel", "id"=>"6"}>
>
> # Change the idea's page to a different one
> >> i.page = Page.find(7)
> => #<Page:0xb79d1b18 @attributes={"scratchpad_id"=>"1",
> "title"=>"Foreign travel", "id"=>"7"}>
>
> # Examine the saved page -- it's the new one
> >> p
> => #<Page:0xb79d1b18 @attributes={"scratchpad_id"=>"1",
> "title"=>"Foreign travel", "id"=>"7"}>
>
> I don't know the rationale. When I (think I'm) assign(ing) an object
> to a variable, I really don't want it to turn out that it's some
> special arrangement whereby the variable is subject to reassignment
> without notice. I expect:
>
> p = i.page
>
> to be exactly equivalent to:
>
> p = Idea.find(i.page.id)
>
> with respect to p.
>
I must be missing something. Isn't this how virtually all Ruby code works?
class Foo
attr_accessor :bar
end
f = Foo.new
f.bar = "some_string"
p = f.bar
f.bar << " and how!"
puts p
What makes the Rails version of this unexpected?