Okay, this is something I run into a lot and don''t really have a great solution. Here is a simple example: class Blog < ActiveRecord::Base has_many :posts validates_presence_of :name end class Post < ActiveRecord::Base belongs_to :blog validates_presence_of :name validates_presence_of :blog_id end Now, if I want to create a blog and post at the same time I don''t want either to be created if one of them fails. So imagine that I have a new valid blog, but a post with no name. I do not want the blog to be created because the post failed validation. Thanks, Tom --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Is this the multiple-models-one-form problem? If so, see this & the prior 2 railscasts: http://railscasts.com/episodes/75-complex-forms-part-3 The problem that''s not treated in those (IIRC) is how to give informative error messages when child item validation fails. IIRC, you get *something* but it''s not super informative... HTH, -Roy -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk@googlegroups.com] On Behalf Of TomRossi7 Sent: Thursday, October 09, 2008 11:50 AM To: Ruby on Rails: Talk Subject: [Rails] has_many validation the Rails way Okay, this is something I run into a lot and don''t really have a great solution. Here is a simple example: class Blog < ActiveRecord::Base has_many :posts validates_presence_of :name end class Post < ActiveRecord::Base belongs_to :blog validates_presence_of :name validates_presence_of :blog_id end Now, if I want to create a blog and post at the same time I don''t want either to be created if one of them fails. So imagine that I have a new valid blog, but a post with no name. I do not want the blog to be created because the post failed validation. Thanks, Tom --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Roy, I have probably 3 different solutions I''ve used in different places, but they each have different drawbacks. The closest solution is the one you refer to, where when the child validates it has an error on the required parent_id, but the parent_id is only missing because the child failed validation! Any beautiful solutions out there? Thanks, Tom On Oct 9, 3:22 pm, "Pardee, Roy" <parde...-go57ItdSaco@public.gmane.org> wrote:> Is this the multiple-models-one-form problem? If so, see this & the prior 2 railscasts: > > http://railscasts.com/episodes/75-complex-forms-part-3 > > The problem that''s not treated in those (IIRC) is how to give informative error messages when child item validation fails. IIRC, you get *something* but it''s not super informative... > > HTH, > > -Roy > > -----Original Message----- > From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk@googlegroups.com] On Behalf Of TomRossi7 > Sent: Thursday, October 09, 2008 11:50 AM > To: Ruby on Rails: Talk > Subject: [Rails] has_many validation the Rails way > > Okay, this is something I run into a lot and don''t really have a great solution. Here is a simple example: > > class Blog < ActiveRecord::Base > has_many :posts > validates_presence_of :name > end > > class Post < ActiveRecord::Base > belongs_to :blog > validates_presence_of :name > validates_presence_of :blog_id > end > > Now, if I want to create a blog and post at the same time I don''t want either to be created if one of them fails. So imagine that I have a new valid blog, but a post with no name. I do not want the blog to be created because the post failed validation. > > Thanks, > Tom--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---