#247 √ stale
Brian Takita

validates_associated - Detailed Validation error messages on associations

Reported by Brian Takita | May 23rd, 2008 @ 07:23 PM

When an associated object (has_one, belongs_to, has_many, etc.) has a validation error, Rails currently gives a non-detailed error message. For example we have a User that has one Profile:

class User < ActiveRecord::Base

has_one :profile

end

If there is a validation error in Profile, the User error message says "Profile is invalid", instead of the actual error message in Profile.

This makes rendering form validation error messages more cumbersome.

I will be attaching a patch that adds a validates_associated method soon.

Comments and changes to this ticket

  • Brian Takita
  • Anton Kuzmin

    Anton Kuzmin May 25th, 2008 @ 05:46 PM

    I used your patch. I was wondering, does user really need to know that his "Profile is invalid"?

    In fact, I see this message twice. I don't know exactly why. Maybe it's because one of my today's hacks when I started trying to understand rails internals for the first time and make the saving of multi-model forms like a breeze.

    Anyway, I uncommented this line:

    record.errors.add(attr_name, ActiveRecord::Errors.default_error_messages[:invalid])
    

    But the 1 message still stayed. It was coming from add_single_associated_save_callbacks method. So I moved a part of your code there. Now I don't even have to call validates_confirmation_of :password

    diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
    index fb5f1f8..6a2d706 100644
    --- a/activerecord/lib/active_record/associations.rb
    +++ b/activerecord/lib/active_record/associations.rb
    @@ -1148,7 +1148,11 @@ module ActiveRecord
               define_method(method_name) do
                 association = instance_variable_get("@#{association_name}")
                 if !association.nil?
    -              errors.add "#{association_name}" unless association.target.nil? || association.valid?
    +              unless association.target.nil? || association.valid?
    +                association.errors.each do |error_name, error_value|
    +                  errors.add(error_name, error_value)
    +                end
    +              end
                 end
               end
    

    So, it's not just one place where detailed validation error message is needed.

    I will continue hacking. Will create a ticket and a patch just after I find it worth sharing.

  • Anton Kuzmin

    Anton Kuzmin May 25th, 2008 @ 09:31 PM

    My damn brain and linux copy-paster :) Sorry

    • Anyway, I uncommented this line:
    • Anyway, I commented this line:
    • Now I don't even have to call validates_confirmation_of :password
    • Now I don't even have to call validates_associated :profile
  • Brian Takita

    Brian Takita May 26th, 2008 @ 02:30 AM

    I'm in favor of not having to validates_associated.

    I think that it would be useful to attach the name of the association that owns the invalid attribute because two models may have the same attribute names.

    This is if the error is attached to object with the association, and not the association that is invalid. Ideally validation errors would be attached to its object and reporting on the validation errors would be recursive. However, it seems like that would be difficult.

    One of the patches attached to this ticket provides an easy to implement interim solution.

  • Joshua Peek

    Joshua Peek August 27th, 2008 @ 02:06 AM

    • → Tag changed from “” to “activerecord”
    • → State changed from “new” to “stale”

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Source available from github

The Git repository resides at http://github.com/rails

Check out the current development trunk (Edge Rails) with:

git clone git://github.com/rails/rails.git

Creating or reviewing a patch

See the contributor guide.

Creating a feature request

Please don't. If you want a new feature in Rails, you'll have to pull up your sleeves and get busy yourself. Or convince someone else to do it. See the contributor guide on how to get going. But posting them here is just going to lead to ticket root.

Creating a bug report

When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.

Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.

Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too".

Shared Ticket Bins

Attachments