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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:41:23+00:00 2026-06-10T22:41:23+00:00

Is there a way to do validations across models within the nested structure of

  • 0

Is there a way to do validations across models within the nested structure of a nested model form?
In the nested hierarchy I’m working with, a child model references an attribute in the parent to perform a validation.
Since the validations are done from the bottom up, (the child model is validated first),
the child does not have a reference to the parent and the validation fails.
For example:

# encoding: utf-8
class Child < ActiveRecord::Base
  attr_accessible :child_attribute
  belongs_to :parent
  validate :to_some_parent_value

  def to_some_parent_value
    if child_attribute > parent.parent_attribute # raises NoMethodError here on ‘parent’
      errors[:child_attribute] << "Validation error."
    end
  end
end

class Parent < ActiveRecord::Base
  attr_accessible :parent_attribute
  has_one :child
  accepts_nested_attributes_for :child
end

In console:

> p=Parent.new( { "parent_attribute" => "1", "child_attributes" => { "child_attribute" => "2" }} )
> p.valid?
=> NoMethodError: undefined method `parent_attribute' for nil:NilClass

Is there a way to have this kind of validation where the child references a value in the parent and still use the Rails nested model forms feature?

  • 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-10T22:41:25+00:00Added an answer on June 10, 2026 at 10:41 pm

    edit: hum, I read your post a bit too fast, I thought that with where the child references a value in the parent, you meant the foreign key parent_id… My answer might still help, not sure.

    I think you’re looking for the inverse_of option. Try that:

    class Parent < ActiveRecord::Base
        has_one :child, inverse_of :parent
    end
    
    class Child < ActiveRecord::Base
        belongs_to :parent, inverse_of :child
    end
    

    From the doc:

    Validating the presence of a parent model

    If you want to validate that a child record is associated with a parent record, you can use validates_presence_of and inverse_of as this example illustrates:

    class Member < ActiveRecord::Base
        has_many :posts, :inverse_of => :member
        accepts_nested_attributes_for :posts
    end
    
    class Post < ActiveRecord::Base
        belongs_to :member, :inverse_of => :posts
        validates_presence_of :member
    end
    
    • 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 retrieve failed validations without checking the error message? If
Is there a way in ASP.NET Webforms to accommodate attribute based validation. If so
I wondering is there a way to represent model differently (or probably control access
Is there any way to assign a group of asp.net validations to validate when
Is there a way to query which attributes on an ActiveRecord model are validated?
Is there a way to write a custom validator that will perform different validations
Is there any way to add validation (ie SSN) to a SharePoint field and
Is there a way to add a validation check for textfield in a UIAlertView
Is there a way to perform data validation using CHECK constraints in t-sql and
Is there a way to apply fade effect after validation. For example the user

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.