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

  • Home
  • SEARCH
  • 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 7641209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:51:57+00:00 2026-05-31T08:51:57+00:00

I have a problem with the scoped uniqueness validation in Rails. It works fine

  • 0

I have a problem with the scoped uniqueness validation in Rails. It works fine if I try to create a new object with the same set of attributes that I don’t want to repeat, directly in the child model, but when I try to create a parent with two childs that are not unique, the validation is not triggered.

Background

I have an application in Rails 3.2, with its views in HAML with simple_form.

I have two models: Page and Property. A page can have many properties, and it accepts nested attributes for property.

I want to validate that a Page must not have two properties with the same name:

#app/models/page.rb
class Page < ActiveRecord::Base
  has_many :properties
  accepts_nested_attributes_for :properties, :allow_destroy => :true
end


#app/models/property.rb
class Property < ActiveRecord::Base
  belongs_to :page
  VALID_PROPERTIES = %w(id text name xpath class css)
  validates :name, :inclusion => VALID_PROPERTIES, :uniqueness => {:scope => :page_id}
end

Of course, the property has a page_id attribute.

Like I said, when creating a new property via its form, the validation works. If I try to create a new property with the same name and the same page_id, Rails tells me that the name has already been taken.

The issue

If I create a new Page, and via nested forms, assign various properties, I am able to bypass this validation. It appears to be only a problem when a combination of page_id and property_id aren’t already present in the database, so for example if I edit a Page model, which already has a property saved, and I try to add a new one with the same name, the validation now triggers.

  • 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-31T08:51:58+00:00Added an answer on May 31, 2026 at 8:51 am

    I would try with validates_associated :

    class Page < ActiveRecord::Base
      has_many :properties
      accepts_nested_attributes_for :properties, :allow_destroy => :true
      validates_associated :properties 
    end
    

    Update

    The Rails guide on validation states:

    The validation happens by performing an SQL query into the model’s
    table, searching for an existing record with the same value in that
    attribute.

    The 2 Properties object that you’re creating do not exists yet in the database, so the uniqueness validation can’t work. You should try with a custom validation

    class Property < ActiveRecord::Base
      #...
      validate :name, :name_uniqueness
    
      def name_uniqueness 
        self.page.properties.select {|p| p.key == self.key}.size == 1
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have problem when I try insert some data to Informix TEXT column via
I have problem creating new instance of excel 2007 using VBA (from Access 2002).
i have problem with autorotate on iphone i set up in all classes -
I have problem with cakephp's Session->write method. If I set a value like $_SESSION['..']
I have a JSF application that makes much use of session-scoped variables. A new
I have a problem to refresh or reload an application scoped managed bean. It
I am a beginner in Rails and i have a problem with scope. I
Currently I have a session scoped object called MySessionData. Originally when logging out I
I have a little problem with my application rails and locale params. I successfully

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.