I''ve experienced several inconsistencies when I traverse related ActiveRecord objects before they are saved into database. For example: c = parent.children.build # => #<Child:0x222222> parent.children # => [#<Child:0x222222>] c.parent.children # => [] .... it should return the same as parent.children My question is this: is it generally a bad idea to interact with object graph that''s unsaved? While it''s common practice to build an object in memory, bounce it back and fourth between the view and the controller all without saving it, I find that kind of paradigm hard to achieve when I need to build multiple objects that are related to each other. It seems to only work well for a single object, in which case the only inconsistency is that it doesn''t have an ID yet. Thoughts? Eric --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Eric Lin wrote:> I''ve experienced several inconsistencies when I traverse related > ActiveRecord objects before they are saved into database. For example: > > c = parent.children.build # => #<Child:0x222222> > parent.children # => [#<Child:0x222222>] > c.parent.children # => [] .... it should return the > same as parent.children > > My question is this: is it generally a bad idea to interact with object > graph that''s unsaved? While it''s common practice to build an object in > memory, bounce it back and fourth between the view and the controller > all without saving it, I find that kind of paradigm hard to achieve when > I need to build multiple objects that are related to each other. It > seems to only work well for a single object, in which case the only > inconsistency is that it doesn''t have an ID yet. > > Thoughts?You can manually stitch things together, such as back links for belongs_to associations, and by adding c.parent = parent after the build in your example. Build should probably do this automatically. -- We develop, watch us RoR, in numbers too big to ignore. --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hmm... that seems kind of inconvenient, but I''ll give it a try, thanks. On Feb 2, 2008 8:46 AM, Mark Reginald James <mrj-bzGI/hKkdgQnC9Muvcwxkw@public.gmane.org> wrote:> > Eric Lin wrote: > > I''ve experienced several inconsistencies when I traverse related > > ActiveRecord objects before they are saved into database. For example: > > > > c = parent.children.build # => #<Child:0x222222> > > parent.children # => [#<Child:0x222222>] > > c.parent.children # => [] .... it should return the > > same as parent.children > > > > My question is this: is it generally a bad idea to interact with object > > graph that''s unsaved? While it''s common practice to build an object in > > memory, bounce it back and fourth between the view and the controller > > all without saving it, I find that kind of paradigm hard to achieve when > > I need to build multiple objects that are related to each other. It > > seems to only work well for a single object, in which case the only > > inconsistency is that it doesn''t have an ID yet. > > > > Thoughts? > > You can manually stitch things together, such as back links for > belongs_to associations, and by adding c.parent = parent after > the build in your example. Build should probably do this > automatically. > > -- > We develop, watch us RoR, in numbers too big to ignore. > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
By the way, is the inability to back reference from child (c.parent.children ) a generally known behavior, or is it something that should be fixed in the framework? On Feb 2, 2008 2:57 PM, Eric Lin <eylinlist-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hmm... that seems kind of inconvenient, but I''ll give it a try, thanks. > > > On Feb 2, 2008 8:46 AM, Mark Reginald James <mrj-bzGI/hKkdgQnC9Muvcwxkw@public.gmane.org> wrote: > > > > > Eric Lin wrote: > > > I''ve experienced several inconsistencies when I traverse related > > > ActiveRecord objects before they are saved into database. For > > example: > > > > > > c = parent.children.build # => #<Child:0x222222> > > > parent.children # => [#<Child:0x222222>] > > > c.parent.children # => [] .... it should return the > > > same as parent.children > > > > > > My question is this: is it generally a bad idea to interact with > > object > > > graph that''s unsaved? While it''s common practice to build an object > > in > > > memory, bounce it back and fourth between the view and the controller > > > all without saving it, I find that kind of paradigm hard to achieve > > when > > > I need to build multiple objects that are related to each other. It > > > seems to only work well for a single object, in which case the only > > > inconsistency is that it doesn''t have an ID yet. > > > > > > Thoughts? > > > > You can manually stitch things together, such as back links for > > belongs_to associations, and by adding c.parent = parent after > > the build in your example. Build should probably do this > > automatically. > > > > -- > > We develop, watch us RoR, in numbers too big to ignore. > > > > > > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---