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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:30:50+00:00 2026-05-13T14:30:50+00:00

I have nested attributes for a Rails model and the associations validations are failing

  • 0

I have nested attributes for a Rails model and the associations validations are failing for some reason. I am not useing accepts_nested_attributes_for, but I am doing something very similar.

class Project < ActiveRecord::Base
  has_many :project_attributes

  def name
    project_attributes.find_by_name("name")
  end

  def name=(val)
    attribute = project_attributes.find_by_name("name")

    if attribute
      attribute.value = val
    else
      project_attributes.build(:name=>"name", :value=>val)
    end
  end
end

class ProjectAttribute < ActiveRecord::Base
  belongs_to :project

  validates_presence_of :name
  validates_uniqueness_of :name, :scope => :project_id

  validates_presence_of :project_id, :unless => lambda {|attribute| attribute.project.try(:valid?)}
  validates_associated :project

end

This is a contrived example, but similar to what I am trying to do. I’ve taken a look at what accepts_nested_attributes_for does and it uses the build method, which I thought would associate the built attribute with the project.

I also looked at accepts_nested_attributes_for child association validation failing which gave me the validates_presence_of :unless=>valid?

Any ideas on how to get this to work?

  • 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-13T14:30:51+00:00Added an answer on May 13, 2026 at 2:30 pm

    validates_associated looks to be more trouble than it’s worth. Your example works if you drop validates_presence_of :project_id. Here is a hacky example that restores that validations (found description here).

    class ProjectAttribute < ActiveRecord::Base
      belongs_to :project
    
      validates_presence_of :name
      validates_uniqueness_of :name, :scope => :project_id
    
      validates_presence_of :project_id, :unless => Proc.new { |project_attribute|
        project = project_attribute.project
        ObjectSpace.each_object(Project) {|o| project = o if o.project_attributes.include?(project_attribute)} unless project
        project
      }
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Rails' accepts_nested_attributes_for method with great success, but how can I have it
I have a sign-up form that has nested associations/attributes whatever you want to call
can anyone please walk me through Nested Attributes in Rails 3? I have two
I have a rails 3 form with nested attributes. So, for example, the parent
I have a Project model which accepts nested attributes for tasks. And Task has
I have previously used has_and_belongs_to_many associations in my older Rails apps but am moving
I'm using Rails' accepts_nested_attributes_for method with great success, but how can I have it
Just wondering if there's a method in rails to identify if nested attributes have
I have some nested tables that I want to hide/show upon a click on
I have a three-level multi-nested form in Rails. The setup is like this: Projects

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.