model attached to model validation

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?

anyone?

You could use conditional validation. http://guides.rubyonrails.org/activerecord_validations_callbacks.html#using-a-symbol-with-if-and-unless That's one way to do it.

Cheers! Arzumy

You can use nested forms (site has_one or has_many 'equipment')

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

erwin

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

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