Chris Roos
2006-Feb-14 10:34 UTC
[Rails] Assigning has_many child in parent creation question
If I have... class Parent has_many :children end class Child belongs_to :parent end ...then... * Assigning a _new_ child during parent''s creation saves the child record with the parent''s id. child = Child.new Parent.create(:children => [child]) # Results in child being associated with parent * Assigning an existing child after a parent''s creation saves the child record with the parent''s id. child = Child.new parent = Parent.new parent.children << child # Results in child being associated with parent ...however... * Assigning an _existing_ child during parent''s creation does not save the child''s record with the parent''s id. child = Child.create Parent.create(:children => [Child.new]) # Child is not associated with parent This seems slightly counter-intuitive; unless I am missing something obvious. Any thoughts? Chris
Byron Saltysiak
2006-Feb-14 11:00 UTC
[Rails] Assigning has_many child in parent creation question
In your final example the "child = Child.create" line is irrelevant, right? "child" is never used. What happens when you do use it? That should definitely work. - Byron On 2/14/06, Chris Roos <chris@seagul.co.uk> wrote:> > If I have... > > class Parent > has_many :children > end > > class Child > belongs_to :parent > end > > ...then... > > * Assigning a _new_ child during parent''s creation saves the child > record with the parent''s id. > child = Child.new > Parent.create(:children => [child]) > # Results in child being associated with parent > > * Assigning an existing child after a parent''s creation saves the child > record with the parent''s id. > child = Child.new > parent = Parent.new > parent.children << child > # Results in child being associated with parent > > ...however... > > * Assigning an _existing_ child during parent''s creation does not save > the child''s record with the parent''s id. > child = Child.create > Parent.create(:children => [Child.new]) > # Child is not associated with parent > > This seems slightly counter-intuitive; unless I am missing something > obvious. Any thoughts? > > Chris > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Byron http://byron.saltysiak.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060214/e5b692d0/attachment.html
Chris Roos
2006-Feb-14 11:14 UTC
[Rails] Re: Assigning has_many child in parent creation question
Sorry, the last example is incorrect - I should be using the new child in my parent construction child = Child.create Parent.create(:children => child) Chris Chris Roos wrote:> If I have... > > class Parent > has_many :children > end > > class Child > belongs_to :parent > end > > ...then... > > * Assigning a _new_ child during parent''s creation saves the child > record with the parent''s id. > child = Child.new > Parent.create(:children => [child]) > # Results in child being associated with parent > > * Assigning an existing child after a parent''s creation saves the child > record with the parent''s id. > child = Child.new > parent = Parent.new > parent.children << child > # Results in child being associated with parent > > ...however... > > * Assigning an _existing_ child during parent''s creation does not save > the child''s record with the parent''s id. > child = Child.create > Parent.create(:children => [Child.new]) > # Child is not associated with parent > > This seems slightly counter-intuitive; unless I am missing something > obvious. Any thoughts? > > Chris-- Posted via http://www.ruby-forum.com/.
Chris Roos
2006-Feb-14 11:17 UTC
[Rails] Re: Assigning has_many child in parent creation question
Err, apparently I''m more stoopid than I realised. This should of course be child = Child.create Parent.create(:children => [child]) Regardless of my ineptitude, this is the example that does not assign the child to the parent (i.e. child.parent_id is null in database). Chris Chris Roos wrote:> Sorry, the last example is incorrect - I should be using the new child > in my parent construction > > child = Child.create > Parent.create(:children => child) > > Chris >-- Posted via http://www.ruby-forum.com/.
Nick Stuart
2006-Feb-14 13:49 UTC
[Rails] Re: Assigning has_many child in parent creation question
Try this... Parent.children << Child.new See what happens... On 2/14/06, Chris Roos <chrisroos@revieworld.com> wrote:> Err, apparently I''m more stoopid than I realised. This should of course > be > > child = Child.create > Parent.create(:children => [child]) > > Regardless of my ineptitude, this is the example that does not assign > the child to the parent (i.e. child.parent_id is null in database). > > Chris > > Chris Roos wrote: > > Sorry, the last example is incorrect - I should be using the new child > > in my parent construction > > > > child = Child.create > > Parent.create(:children => child) > > > > Chris > > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Chris Roos
2006-Feb-14 14:21 UTC
[Rails] Re: Re: Assigning has_many child in parent creation question
I probably should have been slightly more explicit in my initial example. What you suggest works fine. The reason that your method works is the basis for my confusion. With an unsaved child object, both methods (as well as update_attribute/s) work fine. With a previously saved child object, only the assignment on parent creation doesn''t work - all other methods do. This is what seems counter-intuitive to me. Chris -- Posted via http://www.ruby-forum.com/.
Nick Stuart
2006-Feb-14 16:15 UTC
[Rails] Re: Re: Assigning has_many child in parent creation question
The initial problem with the your code (I believe) is that while you were assigning the parent/child relationship you weren''t saving the child again once you did so. As far as I know, the only method that will actually save/create the child, or related objects, is the << operator. In other words, if you set the parent_id explicitly then you need to call child.save. Try adding that in your original code after the call to the parent object. -Nick On 2/14/06, Chris Roos <chrisroos@revieworld.com> wrote:> I probably should have been slightly more explicit in my initial > example. > > What you suggest works fine. The reason that your method works is the > basis for my confusion. > > With an unsaved child object, both methods (as well as > update_attribute/s) work fine. > > With a previously saved child object, only the assignment on parent > creation doesn''t work - all other methods do. This is what seems > counter-intuitive to me. > > Chris > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Anthony Green
2006-Feb-14 20:02 UTC
[Rails] Re: Re: Assigning has_many child in parent creation question
> With a previously saved child object, only the assignment on parent > creation doesn''t work - all other methods do. This is what seems > counter-intuitive to me.The Agile book seems to indicate something like child.create_parent(attributes={}) Tony -- Posted via http://www.ruby-forum.com/.
Chris Roos
2006-Feb-15 08:16 UTC
[Rails] Re: Re: Re: Assigning has_many child in parent creation ques
Nick Stuart wrote:> The initial problem with the your code (I believe) is that while you > were assigning the parent/child relationship you weren''t saving the > child again once you did so. As far as I know, the only method that > will actually save/create the child, or related objects, is the << > operator. > > In other words, if you set the parent_id explicitly then you need to > call child.save. Try adding that in your original code after the call > to the parent object. > > -NickThanks for all the pointers folks. Just for clarity; I already knew how to achieve what I required. I guess my question was more about the principle of least surprise. For each method available to ''join'' a parent and child; all work whether or not the child is saved; _except_ the parent creation method. This just surprised me is all. Thanks for the input, Chris -- Posted via http://www.ruby-forum.com/.