# Validate 2 models in 1 form?

**URL:** <https://discuss.rubyonrails.org/t/validate-2-models-in-1-form/25943>\
**Category:** rubyonrails-talk\
**Created:** [March 6, 2008, 8:53pm UTC](https://discuss.rubyonrails.org/t/validate-2-models-in-1-form/25943 "2008-03-06T20:53:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [March 6, 2008, 8:53pm UTC](https://discuss.rubyonrails.org/t/validate-2-models-in-1-form/25943/1 "2008-03-06T20:53:54Z")

</div>

I have 2 models, an artist and song model

I have the validates\_presence\_of on each of the models (for checking title, artist name, and etc). They validate fine when you add items on their own controller (1 form for each model),

but my program doesn't validate when I combine both models in 1 form.

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [March 6, 2008, 9:42pm UTC](https://discuss.rubyonrails.org/t/validate-2-models-in-1-form/25943/2 "2008-03-06T21:42:35Z")

</div>

1. Maybe There's no attribute in one of your model, you can use attr\_accessor. 2. Have you tried like this ?

---

<div class="post-metadata">

**Author:** ![Mike\_Garey](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@Mike\_Garey](https://discuss.rubyonrails.org/u/Mike_Garey)\
**Post date:** [March 6, 2008, 9:47pm UTC](https://discuss.rubyonrails.org/t/validate-2-models-in-1-form/25943/3 "2008-03-06T21:47:31Z")

</div>

are artists and songs related? (I assume they are). If so, how? How are you creating each model in your one form? You may want something similar to the following:

class Artist \< AR::Base &nbsp;&nbsp;has\_many :songs &nbsp;&nbsp;validates\_presence\_of :name end

class Song \< AR::Base &nbsp;&nbsp;belongs\_to :artist &nbsp;&nbsp;validates\_presence\_of :title end

class SomeController \< ActionController &nbsp;&nbsp;def new &nbsp;&nbsp;&nbsp;&nbsp;@artist = Artist.new &nbsp;&nbsp;&nbsp;&nbsp;@song = @artist.songs.build &nbsp;&nbsp;end

&nbsp;&nbsp;def create &nbsp;&nbsp;&nbsp;&nbsp;@artist = Artist.new(params[:artist])

&nbsp;&nbsp;&nbsp;&nbsp;# append a new song to the list of songs for the artist &nbsp;&nbsp;&nbsp;&nbsp;# you may want to loop through a collection of songs passed from the form &nbsp;&nbsp;&nbsp;&nbsp;# if you want to provide the ability to add multiple songs at once, or you could build &nbsp;&nbsp;&nbsp;&nbsp;# this into the model &nbsp;&nbsp;&nbsp;&nbsp;@song = @artist.songs.build(params[:songs])

&nbsp;&nbsp;&nbsp;if @artist.save &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# both the artist and its related song objects are valid &nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# either artist was invalid or the song object was &nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end end

and in your view:

views/artists/new.html.erb

\<%= error\_messages\_for :artist, :song %\>

Of course the above gets more complicated if you want to assign multiple songs at a time, but this should at least get you started.

Mike

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [March 6, 2008, 9:56pm UTC](https://discuss.rubyonrails.org/t/validate-2-models-in-1-form/25943/4 "2008-03-06T21:56:17Z")

</div>

Yow... Mike, YOurs is more reasonable than me.

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [March 6, 2008, 11:17pm UTC](https://discuss.rubyonrails.org/t/validate-2-models-in-1-form/25943/5 "2008-03-06T23:17:59Z")

</div>

Feng Tien wrote:

> I have 2 models, an artist and song model
> 
> I have the validates\_presence\_of on each of the models (for checking title, artist name, and etc). They validate fine when you add items on their own controller (1 form for each model),
> 
> but my program doesn't validate when I combine both models in 1 form.

Use

validates\_associated :song # in artist model

validates\_associated :artist # in song model

[http://ar.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html#M000304](http://ar.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html#M000304)

Stephan

---

<div class="post-metadata">

**Author:** ![Stephen\_Celis](https://avatars.discourse-cdn.com/v4/letter/s/7ab992/32.png) [@Stephen\_Celis](https://discuss.rubyonrails.org/u/Stephen_Celis)\
**Post date:** [March 7, 2008, 12:06am UTC](https://discuss.rubyonrails.org/t/validate-2-models-in-1-form/25943/6 "2008-03-07T00:06:44Z")

</div>

Stephan,

Please note in the API:

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [March 7, 2008, 12:20am UTC](https://discuss.rubyonrails.org/t/validate-2-models-in-1-form/25943/7 "2008-03-07T00:20:48Z")

</div>

I see, then can add a field like this

class Artist \< ActiveRecord::Base

&nbsp;&nbsp;:attr\_accessor skip\_validate\_song &nbsp;&nbsp;validates\_associated :song, :if =\> Proc.new { |record| !record.skip\_validate\_song }

end

(and / or equivalent to Song class)

Plus set that in the controller as the case may be.

Stephan
