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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:37:37+00:00 2026-05-28T14:37:37+00:00

I need to write a validation to make sure that new records created in

  • 0

I need to write a validation to make sure that new records created in our application do not have duplicate names. I’m told that validates_uniqueness will not work in this instance because the application already uses it to validate the creation of the original name. This validation will ensure that when the record is updated with a new name, that it too, is original…it’s sort of an anti-scope validation. The query also needs to be case insensitive so I will need to write it using MYSQL’s UPPER() function, which is why I’ll need to use an array query and not a hash.

Here’s a sketch of the pseudo code.

Query the CUSTOMER database.
Check to ensure the UPDATED_RECORD to see if its NAME is unique.
If it’s NOT unique check to see if it’s ID belongs to record that’s being updated.
If it is, that’s OK as it’s the same ID and should be allowed to keep the same name.
If the query finds the same name that has a different ID associated with it, then an error message should be displayed such as “This Name is already in use by XXXX.”

I’m fairly new to Rails and have been having a bear of a time getting this structured correctly.

Here’s some clarification on why I don’t think validates_uniqueness will work here.

I think the issue is having multiple versions of the same record, as long as updated record is a new version of the same record (it has the same record ID), then that’s valid. But if a new record version is updated with a name that’s already used by an existing record (with a different record ID, then that shouldn’t be allowed. For example, having two term version records, {:record_id => 100, :name => “Test”} & {:record_id => 100, :name => “Test”}, should be allowed since those two records are versions of the same term. But having two records, {:record_id => 100, :name => “Test”} & {:record_id => 201, :name => “Test”}, should not be allowed since that would result in two versions sharing the same name but associated with different terms.

  • 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-28T14:37:38+00:00Added an answer on May 28, 2026 at 2:37 pm

    I don’t understand why you can’t use validates_uniqueness. You said it’s already used to validate the creation of the original name, which I am guessing is a column different than the one you want to now check, but that doesn’t matter.

    class MyModel < ActiveRecord::Base
        validates_uniqueness_of :original_name
        validates_uniqueness_of :new_name, :case_sensitive => false
    end
    

    When you create a new record, or update an existing one, these validations will pass. You can also set a uniqueness validation in the migration by adding an index on it. This gives extra security.

    EDIT

    Regarding your clarification, it might be easier to add a custom validation, as follows:

    class MyModel < ActiveRecord::Base
        validate :check_names
    
        private
    
        def check_names
            existing = MyModel.find_by_name(self.name)
            unless existing.nil?
                if existing.record_id != self.record_id
                    self.errors[:base] << "This name already exists for a different record_id"
                end
            end
        end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a Delphi application that pulls entries up from various tables
I need write an update statement that used multiple tables to determine which rows
I need to write a program used internally where different users will have different
I need to write a Java Comparator class that compares Strings, however with one
I need to write a web application using SQL Server 2005, asp.net, and ado.net.
I need to write code that picks up PGP-encrypted files from an FTP location
I need to write an extension method on a byte[]. Is that possible?
I need to write test cases for my application. I've chosen NUnit. Please let
We need to write a email validation program in C. We are planning to
I have a project where I need to pull in a lot of records

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.