# Validates\_associated

**URL:** <https://www.ruby-forum.com/t/validates-associated/65635>\
**Category:** Rails\
**Created:** [August 20, 2006, 1:21am UTC](https://www.ruby-forum.com/t/validates-associated/65635 "2006-08-20T01:21:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![welly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f0a364/32.png) [@welly](https://www.ruby-forum.com/u/welly)\
**Post date:** [August 20, 2006, 1:21am UTC](https://www.ruby-forum.com/t/validates-associated/65635/1 "2006-08-20T01:21:58Z")

</div>

Hello all,

Having a bit of a problem with validates\_associated. I assume I’m  
missing something out but basically got these two models -

class Seller \< User

has\_one :profile, :foreign\_key =\> ‘user\_id’, :dependent =\> :destroy  
has\_many :properties

validates\_associated :profile

end

class Profile \< ActiveRecord::Base

belongs\_to :user  
validates\_presence\_of :title, :forename, :surname, :address\_a

end

My form looks like

## Register as seller

\<%= error\_messages\_for :user %\>  
\<% form\_for :user do |f| -%\>

Login  
 \<%= f.text\_field :login %\>

Email  
 \<%= f.text\_field :email %\>

Password  
 \<%= f.password\_field :password %\>

Confirm Password  
 \<%= f.password\_field :password\_confirmation %\>

\<% fields\_for :profile do |p| %\>

Title  
 Mrs Mr Miss Ms Dr

Forename  
 \<%= p.text\_field :forename %\>

Surname  
 \<%= p.text\_field :surname %\>

Address  
 \<%= p.text\_field :address\_a %\>

Address  
 \<%= p.text\_field :address\_b %\>

Town/City  
 \<%= p.text\_field :town %\>

County  
 \<%= county\_options\_for\_select %\>

Postcode  
 \<%= p.text\_field :postcode %\>

Subscribe to mailing list  
 \<%= p.check\_box :mailing\_list %\>

\<% end %\>

\<%= submit\_tag 'Sign up' %\>

\<% end -%\>

Basically when I submit the form with the missing fields required by  
the Profile model, the form submits anyway.

Any suggestions?

Thanks in advance,

Alastair

* * *

Alastair M.  
Standards compliant web development with Ruby On Rails, PHP and ASP  
[www.kozmo.co.uk](http://www.kozmo.co.uk)  
07738 399038

---

<div class="post-metadata">

**Author:** ![welly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f0a364/32.png) [@welly](https://www.ruby-forum.com/u/welly)\
**Post date:** [August 20, 2006, 1:52am UTC](https://www.ruby-forum.com/t/validates-associated/65635/2 "2006-08-20T01:52:01Z")

</div>

On 8/19/06, Alastair M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> validates\_associated :profile
> 
> end

You also want  
validates\_presence\_of :profile

jeremy

---

<div class="post-metadata">

**Author:** ![welly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f0a364/32.png) [@welly](https://www.ruby-forum.com/u/welly)\
**Post date:** [August 20, 2006, 3:32am UTC](https://www.ruby-forum.com/t/validates-associated/65635/3 "2006-08-20T03:32:06Z")

</div>

Hi Jeremy,

That seems to result in “Profile can’t be blank” whether all the  
fields are filled or not. Any ideas?

Thanks!

Alastair

On 20 Aug 2006, at 00:36, Jeremy K. wrote:

> validates\_associated :profile
> 
> end
> 
> You also want  
> validates\_presence\_of :profile
> 
> jeremy
> 
> >

* * *

Alastair M.  
Standards compliant web development with Ruby On Rails, PHP and ASP  
[www.kozmo.co.uk](http://www.kozmo.co.uk)  
07738 399038

---

<div class="post-metadata">

**Author:** ![welly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f0a364/32.png) [@welly](https://www.ruby-forum.com/u/welly)\
**Post date:** [August 20, 2006, 11:46am UTC](https://www.ruby-forum.com/t/validates-associated/65635/4 "2006-08-20T11:46:32Z")

</div>

On 20 Aug 2006, at 02:56, Jeremy K. wrote:

> On 8/19/06, Alastair M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
> Hi Jeremy,
> 
> That seems to result in “Profile can’t be blank” whether all the  
> fields are filled or not. Any ideas?
> 
> Assign the profile to the user before validating the user.

Thanks again, missed that bit! It was late. All working now.

Cheers,

Alastair

* * *

Alastair M.  
Standards compliant web development with Ruby On Rails, PHP and ASP  
[www.kozmo.co.uk](http://www.kozmo.co.uk)  
07738 399038

---

<div class="post-metadata">

**Author:** ![welly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f0a364/32.png) [@welly](https://www.ruby-forum.com/u/welly)\
**Post date:** [August 20, 2006, 4:12am UTC](https://www.ruby-forum.com/t/validates-associated/65635/5 "2006-08-20T04:12:08Z")

</div>

On 8/19/06, Alastair M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi Jeremy,  
> That seems to result in “Profile can’t be blank” whether all the fields  
> are filled or not. Any ideas?

Assign the profile to the user before validating the user.

Best,  
jeremy

---

<div class="post-metadata">

**Author:** ![welly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f0a364/32.png) [@welly](https://www.ruby-forum.com/u/welly)\
**Post date:** [August 20, 2006, 6:59pm UTC](https://www.ruby-forum.com/t/validates-associated/65635/6 "2006-08-20T18:59:04Z")

</div>

Alastair, could you post your fixed code? I’m not sure I follow what  
you changed to make it work.

Thanks,  
Ben

---

<div class="post-metadata">

**Author:** ![welly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f0a364/32.png) [@welly](https://www.ruby-forum.com/u/welly)\
**Post date:** [August 20, 2006, 7:23pm UTC](https://www.ruby-forum.com/t/validates-associated/65635/7 "2006-08-20T19:23:07Z")

</div>

On 20 Aug 2006, at 17:40, Ben L. wrote:

> Alastair, could you post your fixed code? I’m not sure I follow what  
> you changed to make it work.
> 
> Thanks,  
> Ben

Hi Ben,

It wasn’t my models that were incorrect, I had missed some code in  
the controller that linked the user with it’s user profile.

Basically this: @profile = @user.profile = Profile.new(params  
[:profile]) fixed the problem. It wasn’t the validates, it was that  
the user and it’s profile were unrelated.

Cheers,

Alastair

* * *

Alastair M.  
Standards compliant web development with Ruby On Rails, PHP and ASP  
[www.kozmo.co.uk](http://www.kozmo.co.uk)  
07738 399038
