# Validate 2 models in 1 form?

**URL:** <https://www.ruby-forum.com/t/validate-2-models-in-1-form/131203>\
**Category:** Rails\
**Created:** [March 6, 2008, 9:53pm UTC](https://www.ruby-forum.com/t/validate-2-models-in-1-form/131203 "2008-03-06T21:53:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pood](https://www.ruby-forum.com/u/pood)\
**Post date:** [March 6, 2008, 9:53pm UTC](https://www.ruby-forum.com/t/validate-2-models-in-1-form/131203/1 "2008-03-06T21: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:** ![pood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pood](https://www.ruby-forum.com/u/pood)\
**Post date:** [March 6, 2008, 10:42pm UTC](https://www.ruby-forum.com/t/validate-2-models-in-1-form/131203/2 "2008-03-06T22: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 ?

* * *

## Controller :

def new\_song  
#it uses Song Model  
@bad\_song = Artist.new

#it uses Artis Model  
@bad\_artist = Artist.new  
end

def record\_it  
@bad\_song = Song.new(params[:song])  
@bad\_artist = Artist.new(params[:artist])

if @bad\_song.save  
else  
flash[:notice] = “Error…”  
end

if @bad\_artist.save  
else  
flash[:notice] = “Error…”  
end

end

* * *

## View :

\<%#= comment your error\_messages\_for ‘song’ %\>  
\<%#= comment your error\_messages\_for ‘artist’ %\>  
\<%#= they will show you double tables error message %\>

\<% form\_tag :action =\> ‘record\_it’ do %\>  
\<%= text\_field ‘song’, ‘title’ %\>  
\<%= text\_field ‘artist’, ‘artist\_name’ %\>  
\<%= submit\_tag “save” %\>  
\<% end %\>

Have Nice Try and Good Luck. Would You mind to provide us your code so  
we can see your trouble location.

```auto
Reinhart Ariando
WEB: Teapoci.Blogspot.com
YM : Booking2Heaven
MSN: removed_email_address@domain.invalid

```

---

<div class="post-metadata">

**Author:** ![pood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pood](https://www.ruby-forum.com/u/pood)\
**Post date:** [March 6, 2008, 10:48pm UTC](https://www.ruby-forum.com/t/validate-2-models-in-1-form/131203/3 "2008-03-06T22:48:20Z")

</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  
has\_many :songs  
validates\_presence\_of :name  
end

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

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

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

```
# append a new song to the list of songs for the artist
# you may want to loop through a collection of songs passed from the 

```

form  
# if you want to provide the ability to add multiple songs at  
once, or you could build  
# this into the model  
@song = @artist.songs.build(params[:songs])

if @artist.save  
# both the artist and its related song objects are valid  
else  
# either artist was invalid or the song object was  
end  
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

On Thu, Mar 6, 2008 at 3:53 PM, Feng T.

---

<div class="post-metadata">

**Author:** ![pood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pood](https://www.ruby-forum.com/u/pood)\
**Post date:** [March 7, 2008, 12:17am UTC](https://www.ruby-forum.com/t/validate-2-models-in-1-form/131203/4 "2008-03-07T00:17:59Z")

</div>

Feng T. 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:** ![pood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pood](https://www.ruby-forum.com/u/pood)\
**Post date:** [March 7, 2008, 1:07am UTC](https://www.ruby-forum.com/t/validate-2-models-in-1-form/131203/5 "2008-03-07T01:07:05Z")

</div>

Stephan,

Please note in the API:

> …this would specify a circular dependency and cause infinite recursion.  
> On Mar 6, 5:17 pm, Stephan W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![pood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pood](https://www.ruby-forum.com/u/pood)\
**Post date:** [March 6, 2008, 10:56pm UTC](https://www.ruby-forum.com/t/validate-2-models-in-1-form/131203/6 "2008-03-06T22:56:17Z")

</div>

Yow… Mike, YOurs is more reasonable than me.

---

<div class="post-metadata">

**Author:** ![pood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pood](https://www.ruby-forum.com/u/pood)\
**Post date:** [March 7, 2008, 1:20am UTC](https://www.ruby-forum.com/t/validate-2-models-in-1-form/131203/7 "2008-03-07T01:20:48Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Stephan,
> 
> Please note in the API:
> 
> > …this would specify a circular dependency and cause infinite recursion.  
> > On Mar 6, 5:17 pm, Stephan W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

I see, then can add a field like this

class Artist \< ActiveRecord::Base

:attr\_accessor skip\_validate\_song  
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
