Not sure that this is the best way but how about mixing the two? c = Child.new(params) p.children.build(params) unless (some conditional stuff on c)
Regards, Kerry Glenn Little wrote: > I'm looking for a way to add new child objects to a parent's has_many > collection, but without doing an immediate save. > > There is build(): > > p = Parent.new(stuff) > p.children.build(params) > > But the problem is I'd like to do this conditionally without having to > pick apart params. I'd do: > > c = Child.new(params) > p.children << c unless (some conditional stuff on c) > > or > > p.children.push(c) unless (some conditional stuff on c) > > but both of those immediately save the child object c. > > Lastly, associating the child to the parent via the child: > > p = Parent.new(stuff) > c = Child.new(params) > c.parent = p unless (some conditional stuff on c) > > leaves the parent unaware of the child in its p.children collection. > > Is there a clean rails-y idiom I'm missing? > > Thanks much... > > -glenn > > > > > --~--~---------~--~----~------------~-------~--~----~ SD Ruby mailing list [email protected] http://groups.google.com/group/sdruby -~----------~----~----~----~------~----~------~--~---
