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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:13:08+00:00 2026-06-05T21:13:08+00:00

I’m having some problem with validations for multiple fields, specifically with case-sensitive unique validations

  • 0

I’m having some problem with validations for multiple fields, specifically with case-sensitive unique validations as well as the error message.

Let’s say I want to ensure that the following field combinations are unique and are case-sensitive (Mary == mary) in my [Postgres] db:

user_id
first_name
last_name

As an example
{4,Mary,Smith}
{4,mary,Smith}
should not save.

I know I need to do 2 things:
1. Create unique index in db
2. validate_uniqueness_of in my model

  1. For the index, I created it though I did not find a way to ensure case-sensitivity (from my reading, it seems that MySQL is while Postgres is not). I’ll consider this done unless someone has an idea of how to do this.
  2. I have the following code in my model:
     
   validates_uniqueness_of :user_id,
                :case_sensitive => false,
                :scope => [:first_name, :last_name],
                :if => :name_subcat_is_name?,
                :message => "You have already saved this contact information combination."

The problem is that the case-sensitivity seems to only apply to user_id, and not also first and last names. So how can I apply case-sensitivity to the validation for all the fields?

Also, my error msg includes the name of the model:

User You have already saved this contact information combination.

I tried using yaml but it didn’t remove the “User” class.
I found this but don’t know how to make it check all my fields rather than just one. How can I add all the fields into a scope and see if that scope is “:taken”?

UPDATE 1 June 24, 2012:

Using my own validation method sounds like exactly what I need but I’m having some trouble implementing the code. The MyModel.find(:first) method has been depreciated, so I tried substituting it with this:

existing_record = ContactInfo.where(:conditions => ["first_name ILIKE ? AND last_name ILIKE ?", first_name, last_name]) 

But when it gets to the next line

unless existing_record.blank? 

I get an error:

PG::Error: ERROR:  column contact_infos.conditions does not exist
LINE 1: SELECT COUNT(*) FROM "contact_infos"  WHERE "contact_infos"....
SELECT COUNT(*) FROM "contact_infos"  WHERE "contact_infos"."conditions" IN ('first_name ILIKE ? AND last_name ILIKE ?', 'mary', 'Smith')
  • 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-05T21:13:10+00:00Added an answer on June 5, 2026 at 9:13 pm

    You could define your own validation method to do this. It could look something like:

    validate :user_id_uniqueness
    
    def user_id_uniqueness
      return unless name_subcat_is_name?
    
      existing_record = MyModel.find(:first, :conditions => ["first_name ILIKE ? AND last_name ILIKE ?", first_name, last_name])
      unless existing_record.blank?
        errors.add(:user_id, "has already been saved in this contact information combination")
      end
    end
    

    (‘ILIKE’ is specific to Postgresql)

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Specifically, suppose I start with the string string =hello \'i am \' me And
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I am currently running into a problem where an element is coming back from
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.