Frederick Cheung
2008-Jul-21 23:13 UTC
unintuitive behaviour of AssocationCollection#first
A post on rails-talk led me to worry about the following: post.comments.first.title = ''Foo'' post.comments.collect &:author #this line isn''t really important - anything that requires loading the collection will do Ta-da! post.comments.first.title reverts to its old value (because it''s now a different object - the first item of the freshly loaded collection as opposed to just the result of find :first). This behaviour is rather unintuitive to me (and I would have thought conducive to hard to track down bugs) - Thoughts? Fred --~--~---------~--~----~------------~-------~--~----~ 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
2008-Jul-22 09:41 UTC
Re: unintuitive behaviour of AssocationCollection#first
> identity map ?It''s a way of guaranteeing that there''s only one object with a given id in a given context: http://martinfowler.com/eaaCatalog/identityMap.html -- 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Jul-22 09:48 UTC
Re: unintuitive behaviour of AssocationCollection#first
On 22 Jul 2008, at 10:41, Michael Koziarski wrote:> >> identity map ? > > It''s a way of guaranteeing that there''s only one object with a given > id in a given context: > > http://martinfowler.com/eaaCatalog/identityMap.html >Ah yes. I had an inkling that might be what you were talking about. That itself might introduce some subtle differences in behaviour Fred --~--~---------~--~----~------------~-------~--~----~ 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
2008-Jul-22 09:52 UTC
Re: unintuitive behaviour of AssocationCollection#first
> Ah yes. I had an inkling that might be what you were talking about. > That itself might introduce some subtle differences in behaviourIt introduces huge differences in behaviour for some situations like: def whatever @foo = Foo.find(1) # Some other process updates foo 1 @foos = Foo.find(:all) # do you retain the old data for foo#1 or replace it with the new data, or raise an exception? end The first part of an identity map is easy, it''s defining the behaviour in all the edge cases that makes it hard, :) -- 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Jul-22 10:05 UTC
Re: unintuitive behaviour of AssocationCollection#first
On 22 Jul 2008, at 10:52, Michael Koziarski wrote:> >> Ah yes. I had an inkling that might be what you were talking about. >> That itself might introduce some subtle differences in behaviour > > It introduces huge differences in behaviour for some situations like: > > def whatever > @foo = Foo.find(1) > # Some other process updates foo 1 > @foos = Foo.find(:all) # do you retain the old data for foo#1 or > replace it with the new data, or raise an exception? > end > > The first part of an identity map is easy, it''s defining the behaviour > in all the edge cases that makes it hard, :)That''s a bit of a bastard :-) It''s not even that much of an edge case. Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---