Given the two tables Parent, Child where Parent has_many children: If I don''t save parent first then it seems that child.parent will not be assigned automatically if I call parent.children << child . Must the parent record be saved before I call parent.children << child ? If I must save Parent first then can someone explain to me how I can call parent.valid? before it is saved and have the children validated too? Any workaround I see requires a lot of coercing of these relationships together over and over again. -John -- John Smilanick Computing Staff - Webmaster Kavli Institute for Theoretical Physics University of California, Santa Barbara jsmilani@kitp.ucsb.edu (805) 893-6307 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060314/ff345d10/attachment.html
Am Dienstag, den 14.03.2006, 14:16 -0800 schrieb John Smilanick:> Given the two tables Parent, Child where Parent has_many children: > If I don''t save parent first then it seems that child.parent will not > be assigned automatically if I call parent.children << child . Must > the parent record be saved before I call parent.children << child ?As Child belongs_to Parent, the foreign key is stored in the children table. If Parent is a new record and has no id, the children can''t save it as foreign key.> If I must save Parent first then can someone explain to me how I can > call parent.valid? before it is saved and have the children validated > too?if !parent.valid? || children.all? { |c| c.valid? } # some records are not valid else parent.save parent.children << *children end So you never get a saved parent unless all children are valid, too. -- Norman Timmler http://blog.inlet-media.de
Using that form of validation means I cannot validate whether a child has a parent or not. Isn''t that a problem? -John -- John Smilanick Computing Staff - Webmaster Kavli Institute for Theoretical Physics University of California, Santa Barbara jsmilani@kitp.ucsb.edu (805) 893-6307 On Mar 15, 2006, at 1:45 AM, Norman Timmler wrote:> Am Dienstag, den 14.03.2006, 14:16 -0800 schrieb John Smilanick: >> Given the two tables Parent, Child where Parent has_many children: >> If I don''t save parent first then it seems that child.parent will not >> be assigned automatically if I call parent.children << child . Must >> the parent record be saved before I call parent.children << child ? > > As Child belongs_to Parent, the foreign key is stored in the children > table. If Parent is a new record and has no id, the children can''t > save > it as foreign key. > >> If I must save Parent first then can someone explain to me how I can >> call parent.valid? before it is saved and have the children validated >> too? > > if !parent.valid? || children.all? { |c| c.valid? } > # some records are not valid > else > parent.save > parent.children << *children > end > > So you never get a saved parent unless all children are valid, too. > > -- > Norman Timmler > > http://blog.inlet-media.de > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060315/acc56cc1/attachment-0001.html