Hello, I got a form that feeds data to 2 models: Topic and Post. @topic = Topic.new(params[:topic]) @post = Post.new(params[:post]) if @topic.save @post.topic_id = @topic.id @post.save end Now if there is an error in the Topic data RoR will catch it. But if the Post fails validation the Topic still gets created. So what would like to do is doing a test on each object and then save. Did google the topic but did not hit the nail on the head. Best regards. Asbjørn Morell. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 24 Aug 2008, at 19:35, atmorell <atmorell-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hello, > > I got a form that feeds data to 2 models: Topic and Post. > > @topic = Topic.new(params[:topic]) > @post = Post.new(params[:post]) > > if @topic.save > > @post.topic_id = @topic.id > @post.save > > end > > Now if there is an error in the Topic data RoR will catch it. But if > the Post fails validation the Topic still gets created. So what would > like to do is doing a test on each object and then save. >Just call the valid? method Fred> Did google the topic but did not hit the nail on the head. > > Best regards. > Asbjørn Morell. > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hmmm just gave it a try: if @post.valid? and @topic.valid? @topic.save @post.topic_id = @topic.id @post.save end The "if" condition does not seem to care about the Post validation. Topic validation works as it should :/ Any ideas? On Aug 24, 8:53 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 24 Aug 2008, at 19:35, atmorell <atmor...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > > > Hello, > > > I got a form that feeds data to 2 models: Topic and Post. > > > @topic = Topic.new(params[:topic]) > > @post = Post.new(params[:post]) > > > if @topic.save > > > -h1xIVYyanLP9KIQWOO/q1A@public.gmane.org_id = @topic.id > > -IBUbae3Hox6zQB+pC5nmwQ@public.gmane.org > > > end > > > Now if there is an error in the Topic data RoR will catch it. But if > > the Post fails validation the Topic still gets created. So what would > > like to do is doing a test on each object and then save. > > Just call the valid? method > > Fred > > > > > Did google the topic but did not hit the nail on the head. > > > Best regards. > > Asbjørn Morell.- Hide quoted text - > > - Show quoted text -- Hide quoted text - > > - Show quoted text ---~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Problem solved. valid? was the method I was looking for. Thanks. On Aug 24, 9:19 pm, atmorell <atmor...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hmmm just gave it a try: > > if @post.valid? and @topic.valid? > > @topic.save > @post.topic_id = @topic.id > @post.save > > end > > The "if" condition does not seem to care about the Post validation. > Topic validation works as it should :/ > > Any ideas? > > On Aug 24, 8:53 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > > > On 24 Aug 2008, at 19:35, atmorell <atmor...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Hello, > > > > I got a form that feeds data to 2 models: Topic and Post. > > > > @topic = Topic.new(params[:topic]) > > > @post = Post.new(params[:post]) > > > > if @topic.save > > > > -h1xIVYyanLP9KIQWOO/q1A@public.gmane.org_id = @topic.id > > > -IBUbae3Hox6zQB+pC5nmwQ@public.gmane.org > > > > end > > > > Now if there is an error in the Topic data RoR will catch it. But if > > > the Post fails validation the Topic still gets created. So what would > > > like to do is doing a test on each object and then save. > > > Just call the valid? method > > > Fred > > > > Did google the topic but did not hit the nail on the head. > > > > Best regards. > > > Asbjørn Morell.- Hide quoted text - > > > - Show quoted text -- Hide quoted text - > > > - Show quoted text -- Hide quoted text - > > - Show quoted text ---~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Sun, Aug 24, 2008 at 8:35 PM, atmorell <atmorell-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I got a form that feeds data to 2 models: Topic and Post. > > @topic = Topic.new(params[:topic]) > @post = Post.new(params[:post]) > > if @topic.save > > @post.topic_id = @topic.id > @post.save > > end > > Now if there is an error in the Topic data RoR will catch it. But if > the Post fails validation the Topic still gets created. So what would > like to do is doing a test on each object and then save.You could for example add validates_associated :topic to the Post model and then: post = Post.new(params[:post]) post.build_topic(params[:topic]) if post.save # both post and topic are valid else # at least one of them is invalid, you got all errors in one shot end That said, in AR I normally work from the parent downwards: topic = Topic.new(params[:topic]) topic.posts.build(params[:post]) topic.save In any case, if the associated object is invalid you have an error in the "root" object telling so, and the particular errors in the child. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Sun, Aug 24, 2008 at 11:40 PM, Xavier Noria <fxn-xlncskNFVEJBDgjK7y7TUQ@public.gmane.org> wrote:> That said, in AR I normally work from the parent downwards: > > topic = Topic.new(params[:topic]) > topic.posts.build(params[:post]) > topic.saveI forgot to mention in that alternative you put validates_associated :posts in the Topic model. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
ascherkus+rails-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Aug-25 13:58 UTC
Re: Validating form data without saving
Watch out: "and" is not the same as "&&" if @post.valid? and @topic.valid? # equivalent to @post.valid?; if @topic.valid? See http://www.pjhyett.com/posts/201-using-or-and-in-ruby for more details. On Aug 24, 3:19 pm, atmorell <atmor...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hmmm just gave it a try: > > if @post.valid? and @topic.valid? > > @topic.save > @post.topic_id = @topic.id > @post.save > > end > > The "if" condition does not seem to care about the Post validation. > Topic validation works as it should :/ > > Any ideas? > > On Aug 24, 8:53 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > On 24 Aug 2008, at 19:35, atmorell <atmor...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Hello, > > > > I got a form that feeds data to 2 models: Topic and Post. > > > > @topic = Topic.new(params[:topic]) > > > @post = Post.new(params[:post]) > > > > if @topic.save > > > > -h1xIVYyanLP9KIQWOO/q1A@public.gmane.org_id = @topic.id > > > -IBUbae3Hox6zQB+pC5nmwQ@public.gmane.org > > > > end > > > > Now if there is an error in the Topic data RoR will catch it. But if > > > the Post fails validation the Topic still gets created. So what would > > > like to do is doing a test on each object and then save. > > > Just call the valid? method > > > Fred > > > > Did google the topic but did not hit the nail on the head. > > > > Best regards. > > > Asbjørn Morell.- Hide quoted text - > > > - Show quoted text -- Hide quoted text - > > > - Show quoted text ---~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I think you really just want a transaction here. If you don''t want the Topic created when the post fails, you can do the following: Post.transaction do @topic = Topic.new(params[:topic]) @post = Post.new(params[:post]) if @topic.save @post.topic_id = @topic.id @post.save end end If one fails, all fail and you don''t get the unwanted saves. -Kevin On Aug 24, 1:35 pm, atmorell <atmor...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello, > > I got a form that feeds data to 2 models: Topic and Post. > > @topic = Topic.new(params[:topic]) > @post = Post.new(params[:post]) > > if @topic.save > > @post.topic_id = @topic.id > @post.save > > end > > Now if there is an error in the Topic data RoR will catch it. But if > the Post fails validation the Topic still gets created. So what would > like to do is doing a test on each object and then save. > > Did google the topic but did not hit the nail on the head. > > Best regards. > Asbjørn Morell.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Tue, Aug 26, 2008 at 5:38 PM, Kevin Whinnery <Kevin.Whinnery-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Post.transaction do > @topic = Topic.new(params[:topic]) > @post = Post.new(params[:post]) > > if @topic.save > @post.topic_id = @topic.id > @post.save > end > end > > If one fails, all fail and you don''t get the unwanted saves.Problem is if @topic.save fails and @post is invalid you are not able to show @post''s validation errors to the user in the same view. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---