# Multi Model Forms and Validations

**URL:** <https://www.ruby-forum.com/t/multi-model-forms-and-validations/164913>\
**Category:** Rails\
**Created:** [April 7, 2009, 11:27am UTC](https://www.ruby-forum.com/t/multi-model-forms-and-validations/164913 "2009-04-07T11:27:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sshefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@sshefer](https://www.ruby-forum.com/u/sshefer)\
**Post date:** [April 7, 2009, 11:27am UTC](https://www.ruby-forum.com/t/multi-model-forms-and-validations/164913/1 "2009-04-07T11:27:37Z")

</div>

Jim N.'s walkthru ([http://jimneath.org/2008/09/06/multi-model-forms-](http://jimneath.org/2008/09/06/multi-model-forms-)  
validations-in-ruby-on-rails/) talks about validating multiple objects  
before saving. His example is below:

if @person.valid? & @cat.valid? & @dog.valid?  
Person.transaction do  
@person.save!  
@cat.save!  
@dog.save!  
end  
else  
FAIL  
end

I am trying to do something similar but in my situation there may not  
always be a @cat or @dog (there will always be a @person though).  
Does anyone know of a way that I can run the same validation but allow  
for the conditional presence of the 2 objects?

Thanks.

---

<div class="post-metadata">

**Author:** ![sshefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@sshefer](https://www.ruby-forum.com/u/sshefer)\
**Post date:** [April 8, 2009, 7:04am UTC](https://www.ruby-forum.com/t/multi-model-forms-and-validations/164913/2 "2009-04-08T07:04:20Z")

</div>

Hmmm… Are Cat and Dog associated to the Person model?

> there may not always be a @cat or @dog

Meaning the parameters for these models will be passed in from the  
form but they will be empty? In which case you can have a  
before\_validation callback and check if all the params for these  
models are blank. If they are, then return false. This will still  
throw a “Cat/Dog is invalid” validation error. That can be handled by  
hacking into error\_messages\_for. Its all quite ugly but it works.  
I can tell you more if you can explain the context better.

---

<div class="post-metadata">

**Author:** ![sshefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@sshefer](https://www.ruby-forum.com/u/sshefer)\
**Post date:** [April 8, 2009, 7:19am UTC](https://www.ruby-forum.com/t/multi-model-forms-and-validations/164913/3 "2009-04-08T07:19:32Z")

</div>

Sure.

My scenario is a bit different.

I have an application form that needs to be filled in by a user. If  
the user is not registered on my site I have a few fields that allow  
the user to quickly enter name and email and their account will be  
created when the application is submitted. At the same time, the  
application allows a user to pick from a list of documents they’ve  
uploaded. However, I also have fields incase a user would like to  
upload a document that is not on the list (to save them the hassle of  
going back, uploading and returning to the application).

Therefore, my application will always have a new @application and  
sometimes a new @user or @document. The document model is  
polymorphic. The application belongs\_to the user and has a field for  
document\_id but I did not create an explicit relationship in the  
model.

Thanks for any help.

---

<div class="post-metadata">

**Author:** ![sshefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@sshefer](https://www.ruby-forum.com/u/sshefer)\
**Post date:** [April 8, 2009, 1:16pm UTC](https://www.ruby-forum.com/t/multi-model-forms-and-validations/164913/4 "2009-04-08T13:16:05Z")

</div>

Just wanted to say that someone helped me with the solution here:  
[http://railsforum.com/viewtopic.php?id=29103](http://railsforum.com/viewtopic.php?id=29103)

Apparently, it’s as simple as:

def valid\_or\_nil?( model )  
model.nil? ? true : model.valid?  
end

if @person.valid? & valid\_or\_nil?(@cat) & valid\_or\_nil?(@dog)  
Person.transaction do  
@person.save!  
@cat.save! unless @cat.nil?  
@dog.save! unless @dog.nil?  
end  
else  
FAIL  
end

---

<div class="post-metadata">

**Author:** ![sshefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@sshefer](https://www.ruby-forum.com/u/sshefer)\
**Post date:** [April 9, 2009, 12:36am UTC](https://www.ruby-forum.com/t/multi-model-forms-and-validations/164913/5 "2009-04-09T00:36:44Z")

</div>

Yuck

What’s wrong with

def valid\_or\_nil?(obj)  
obj.nil? or obj.valid?  
end

Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Learn rails: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)
