# Validating form data without saving

**URL:** <https://www.ruby-forum.com/t/validating-form-data-without-saving/146730>\
**Category:** Rails\
**Created:** [August 24, 2008, 8:36pm UTC](https://www.ruby-forum.com/t/validating-form-data-without-saving/146730 "2008-08-24T20:36:13Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![atmorell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@atmorell](https://www.ruby-forum.com/u/atmorell)\
**Post date:** [August 24, 2008, 8:36pm UTC](https://www.ruby-forum.com/t/validating-form-data-without-saving/146730/1 "2008-08-24T20:36:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![atmorell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@atmorell](https://www.ruby-forum.com/u/atmorell)\
**Post date:** [August 24, 2008, 8:53pm UTC](https://www.ruby-forum.com/t/validating-form-data-without-saving/146730/2 "2008-08-24T20:53:47Z")

</div>

On 24 Aug 2008, at 19:35, atmorell [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> @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

---

<div class="post-metadata">

**Author:** ![atmorell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@atmorell](https://www.ruby-forum.com/u/atmorell)\
**Post date:** [August 24, 2008, 9:20pm UTC](https://www.ruby-forum.com/t/validating-form-data-without-saving/146730/3 "2008-08-24T21:20:08Z")

</div>

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 C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![atmorell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@atmorell](https://www.ruby-forum.com/u/atmorell)\
**Post date:** [August 24, 2008, 11:41pm UTC](https://www.ruby-forum.com/t/validating-form-data-without-saving/146730/4 "2008-08-24T23:41:19Z")

</div>

On Sun, Aug 24, 2008 at 8:35 PM, atmorell [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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.

---

<div class="post-metadata">

**Author:** ![atmorell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@atmorell](https://www.ruby-forum.com/u/atmorell)\
**Post date:** [August 24, 2008, 11:43pm UTC](https://www.ruby-forum.com/t/validating-form-data-without-saving/146730/5 "2008-08-24T23:43:39Z")

</div>

On Sun, Aug 24, 2008 at 11:40 PM, Xavier N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> That said, in AR I normally work from the parent downwards:
> 
> topic = Topic.new(params[:topic])  
> topic.posts.build(params[:post])  
> topic.save

I forgot to mention in that alternative you put

validates\_associated :posts

in the Topic model.

---

<div class="post-metadata">

**Author:** ![atmorell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@atmorell](https://www.ruby-forum.com/u/atmorell)\
**Post date:** [August 24, 2008, 11:20pm UTC](https://www.ruby-forum.com/t/validating-form-data-without-saving/146730/6 "2008-08-24T23:20:50Z")

</div>

Problem solved. valid? was the method I was looking for.

Thanks.

---

<div class="post-metadata">

**Author:** ![atmorell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@atmorell](https://www.ruby-forum.com/u/atmorell)\
**Post date:** [August 25, 2008, 7:28pm UTC](https://www.ruby-forum.com/t/validating-form-data-without-saving/146730/7 "2008-08-25T19:28:56Z")

</div>

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](http://www.pjhyett.com/posts/201-using-or-and-in-ruby) for more  
details.

---

<div class="post-metadata">

**Author:** ![atmorell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@atmorell](https://www.ruby-forum.com/u/atmorell)\
**Post date:** [August 26, 2008, 5:39pm UTC](https://www.ruby-forum.com/t/validating-form-data-without-saving/146730/8 "2008-08-26T17:39:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![atmorell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@atmorell](https://www.ruby-forum.com/u/atmorell)\
**Post date:** [August 26, 2008, 5:45pm UTC](https://www.ruby-forum.com/t/validating-form-data-without-saving/146730/9 "2008-08-26T17:45:13Z")

</div>

On Tue, Aug 26, 2008 at 5:38 PM, Kevin W.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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.
