Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6566205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:11:20+00:00 2026-05-25T14:11:20+00:00

I have two model as follows class User < ActiveRecord::Base validates_associated :account end class

  • 0

I have two model as follows

class User < ActiveRecord::Base
 validates_associated :account
end

class Account < ActiveRecord::Base

  belongs_to :user

  #----------------------------------Validations--Start-------------------------
  validates_length_of :unique_url, :within => 2..30 ,:message => "Should be atleast 3 characters long!"
  validates_uniqueness_of :unique_url ,:message => "Already Taken"
  validates_format_of :unique_url,:with => /^([a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])$/ , :message => " Cannot contain special charaters"
  #----------------------------------Validations--End---------------------------
end

Now when I associate an account to a user it says

“Account is invalid”

Instead I want to get the error message directly from that model.
so it should say

"Should be atleast 3 characters long!" or "Already Taken" or " Cannot contain special charaters"

is there a way to do this ?

I don’t want to give a generic message like :

validates_associated :account , :message=>"one of the three validations failed"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:11:20+00:00Added an answer on May 25, 2026 at 2:11 pm

    You can write your own custom validator, based on the code for the built-in validator.

    Looking up the source code for validates_associated, we see that it uses the "AssociatedValidator". The source code for that is:

    module ActiveRecord
      module Validations
        class AssociatedValidator < ActiveModel::EachValidator
          def validate_each(record, attribute, value)
            if Array.wrap(value).reject {|r| r.marked_for_destruction? || r.valid?}.any?
              record.errors.add(attribute, :invalid, options.merge(:value => value))
            end
          end
        end
    
        module ClassMethods
          
          def validates_associated(*attr_names)
            validates_with AssociatedValidator, _merge_attributes(attr_names)
          end
        end
      end
    end
    

    So you can use this as an example to create a custom validator that bubbles error messages like this (for instance, add this code to an initializer in config/initializers/associated_bubbling_validator.rb):

    module ActiveRecord
      module Validations
        class AssociatedBubblingValidator < ActiveModel::EachValidator
          def validate_each(record, attribute, value)
            ((value.kind_of?(Enumerable) || value.kind_of?(ActiveRecord::Relation)) ? value : [value]).each do |v|
              unless v.valid?
                v.errors.full_messages.each do |msg|
                  record.errors.add(attribute, msg, options.merge(:value => value))
                end
              end
            end
          end
        end
    
        module ClassMethods
          def validates_associated_bubbling(*attr_names)
            validates_with AssociatedBubblingValidator, _merge_attributes(attr_names)
          end
        end
      end
    end
    

    So you can now validate like so:

    class User < ActiveRecord::Base
     validates_associated_bubbling :account
    end
    

    Also, be sure to add a validate: false in your has_many association, otherwise, Rails will validate the association by default and you’ll end up with two error messages, one given by your new AssociatedBubblingValidator and one generic given by Rails.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

so I have two models: class User < ActiveRecord::Base has_and_belongs_to_many :followed_courses, :class_name => Course
I have two models, Picture and SubmittedPicture as follows: class Picture(models.Model): user = models.ForeignKey(User)
I have two models as follows: class Tag(models.Model): # ... class Paragraph(models.Model): tags =
I have two models in Django like follows(in pseudo code) class Medicine(db.Model): field_1 =
I have two models class Friend(models.Model): me = models.ForeignKey(User) friend = models.ForeignKey(User) remark =
I have a model as follows: public class User : System.Web.Security.MembershipUser { public int
I have two models like this: class OptionsAndFeatures(models.Model): options = models.TextField(blank=True, null=True) entertainment =
I have two models: class Actor(models.Model): name = models.CharField(max_length=30, unique = True) event =
I have a basic ActiveRecord model in which i have two fields that i
I have two models in my Django 1.1.1 application: class UserRequest(models.Model): # blah blah

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.