Validates_associated

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
07738 399038

On 8/19/06, Alastair M. [email protected] wrote:

validates_associated :profile

end

You also want
validates_presence_of :profile

jeremy

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
07738 399038

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

On 8/19/06, Alastair M. [email protected] 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
07738 399038

On 8/19/06, Alastair M. [email protected] 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

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

Thanks,
Ben

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
07738 399038