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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:41:44+00:00 2026-06-13T19:41:44+00:00

How do I manually set error on a nested attribute in Rails 3? The

  • 0

How do I manually set error on a nested attribute in Rails 3?

The following is some example model code that I tried and isn’t working for me.

validate :matching_card_colors

has_many :cards
accepts_nested_attributes_for :card

def matching_card_colors
  color = nil
  cards.each do |card|
    if color.nil?
      color = card.color
    elsif card.color != color
      card.errors.add :color, "does not match other card colors"
    end
  end
end
  • 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-06-13T19:41:46+00:00Added an answer on June 13, 2026 at 7:41 pm

    The secret is in where you put the errors. Check out how validations with autosave associations work for a clue:

    def association_valid?(reflection, record)
      return true if record.destroyed? || record.marked_for_destruction?
    
      unless valid = record.valid?(validation_context)
        if reflection.options[:autosave]
          record.errors.each do |attribute, message|
            attribute = "#{reflection.name}.#{attribute}"
            errors[attribute] << message
            errors[attribute].uniq!
          end
        else
          errors.add(reflection.name)
        end
      end
      valid
    end
    

    Note how the errors aren’t added to the association members, but rather to the record being validated, the offending attribute prefixed with the association name. You should be able to do this as well. Try something like:

    def matching_card_colors
      color = nil
      cards.each do |card|
        if color.nil?
          color = card.color
        elsif card.color != color
          # Note, there's one error for the association.  You could always get fancier by
          # making a note of the incorrect colors and adding the error afterwards, if you like.
          # This just mirrors how autosave_associations does it.
          errors['cards.color'] << "your colors don't match!"
          errors['cards.color'].uniq!
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I manually set a position with some code? Ok Ive got some
I want set error validation to input field manually in controller example: if ($remainTime
I have the following code to manually set the position of a (dynamic) object
I only manually set one cookie on my social network site, but I rely
It's easy to set the build and version numbers manually in Xcode, and I
I have an existing panel where I set the html manually with a variable
I have tableview, where create new rows and set them layout manually. In every
Manually built: [btnRun addTarget:self action:@selector(RunApp:) forControlEvents:UIControlEventTouchUpOutside]; Programmatically built: something of the following like ??
How can I handle manually the 404 error in C#? I want to check
I contribute to and implement some open source projects that use a Maven2 build

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.