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 4030160
In Process

The Archive Base Latest Questions

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

How do I perform validations checks (and insert error messages into an errors structure)

  • 0

How do I perform validations checks (and insert error messages into an errors structure) on an attribute which is has_one association.

If an error has occured in “shirt” or “pants”, how can i access that error?
Will the error be in person.shirt.errors[:color] ?

When i trigger person.save, are validations for person.shirt activated?

When i trigger person.save and there is an error in person.shirt, where will the error message be saved? In person.shirt.errors or in person.errors?

class Person < ActiveRecord::Base
      has_one   : shirt
      has_many  : pants
      validates :name, :presence => true
      validates_length_of :name, :minimum => 3
end

person = Person.new(:name => "JD")
person.shirt.create(:color=> "red")
person.pants.create(:type=> "jeans")
person.valid?
  • 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-20T11:26:12+00:00Added an answer on May 20, 2026 at 11:26 am

    You can validate an association of the model with

    validates_associated :shirt
    

    That way when you call person.save it will trigger the validations of shirt.

    And yes, you can access the association’s error with person.shirt.errors but be sure to do that after triggering validations. For example:

    person = Person.new
    person.errors # => will be empty
    

    This is because validations haven’t been ran yet. So you need to call either save or valid? or any other method that triggers validations.

    person = Person.new
    person.valid?
    person.errors # => will have errors in person
    

    And it’s the same for the associations:

    person.shirt.valid?
    person.shirt.errors
    

    but since you’re validating the associations with validates_associated it will be enough with person.valid? to trigger shirt’s validations.

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

Sidebar

Related Questions

Is there a way to write a custom validator that will perform different validations
Is there any Jquery Plugin to perform the image dimension operation on an image
I have the following code to perform validation of SubmitForm shown below on click
How do I disable Model validation for a single Action in a Controller ?
I have this controller set up for a login: <?php class Login extends Controller
I have some code that consists of a lot (several hundreds of LOC) of
Is there a function for ASP.NET MVC 2 built in data annotation javascript validation
I have a jQuery script that appends a row to a table. In short,
I'm designing a WCF service that will return a response code (such as 0
If using code like the following to impersonate another user, [DllImport(advapi32.dll, SetLastError = true)]

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.