# Model attached to model validation

**URL:** <https://www.ruby-forum.com/t/model-attached-to-model-validation/172407>\
**Category:** Rails\
**Created:** [July 21, 2009, 2:43am UTC](https://www.ruby-forum.com/t/model-attached-to-model-validation/172407 "2009-07-21T02:43:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [July 21, 2009, 2:43am UTC](https://www.ruby-forum.com/t/model-attached-to-model-validation/172407/1 "2009-07-21T02:43:02Z")

</div>

I have a model say “site” and another model “equipment”.

In the Site form I can attach equipment dynamically in the form and  
the equipment has attributes in the :through table.

Is there a way to validate the equipment attributes ONLY when the SITE  
form has been completed?

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [July 22, 2009, 5:58am UTC](https://www.ruby-forum.com/t/model-attached-to-model-validation/172407/2 "2009-07-22T05:58:40Z")

</div>

anyone?

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [July 22, 2009, 7:23am UTC](https://www.ruby-forum.com/t/model-attached-to-model-validation/172407/3 "2009-07-22T07:23:25Z")

</div>

You could use conditional validation.  
[http://guides.rubyonrails.org/activerecord\_validations\_callbacks.html#using-a-symbol-with-if-and-unless](http://guides.rubyonrails.org/activerecord_validations_callbacks.html#using-a-symbol-with-if-and-unless)  
That’s one way to do it.

Cheers!  
Arzumy

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [July 22, 2009, 8:39am UTC](https://www.ruby-forum.com/t/model-attached-to-model-validation/172407/4 "2009-07-22T08:39:57Z")

</div>

You can use nested forms (site has\_one or has\_many ‘equipment’)

> **[\#75 Complex Forms Part 3 - RailsCasts](http://railscasts.com/episodes/75-complex-forms-part-3)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

[http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes](http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes)

erwin

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [July 22, 2009, 4:00pm UTC](https://www.ruby-forum.com/t/model-attached-to-model-validation/172407/5 "2009-07-22T16:00:20Z")

</div>

I do an ajax to add the equipment. The models are not nested.

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [July 22, 2009, 10:41am UTC](https://www.ruby-forum.com/t/model-attached-to-model-validation/172407/6 "2009-07-22T10:41:33Z")

</div>

I would say that in your case the easy way is to not nest the forms.  
Just return in your post the list of equipments and the site. In your  
controller, save or update the site, then try to save each equipment.  
Make sure you reassign your instance variables properly.

If you want to keep them nested and do this I would suggest submit the  
site the first time ‘add equipment’ or whatever button is pressed and  
reload the page or AJAX it up in the background.

-Josh
