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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:03:59+00:00 2026-06-08T03:03:59+00:00

I have a model with field a, b. I want to make sure that

  • 0

I have a model with field a, b. I want to make sure that the combination of a and b is unique in the table, so a=1, b=2 and a=2, b=1 would raise a conflict.

I have try validates_uniqueness_of :a, :scope => :b, but it only make sure that no two
a=1, b=2 row can exist, I also want to filter out the a=2, b=1. Thanks

  • 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-08T03:04:00+00:00Added an answer on June 8, 2026 at 3:04 am

    The built-in uniqueness validator does not support that, as far as I know. You would have to write a custom validation for that, e.g.:

    class MyModel < AR::Base
    
      validate :my_custom_validation
    
      def my_custom_validation
        if self.class.where("(a = :a AND b = :b) OR (a = :b AND b = :a)", {:a => a, :b => b}).exists?
          errors.add(:a, :taken)
        end
      end
    end
    

    This will add a validation error to attribute a if there is any record having (a = 1 AND b = 2) OR (a = 2 AND b = 1).

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

Sidebar

Related Questions

I want a Django model to have a date field in which the year,
I want to have a model with calculated fields that I can apply sorting
I have model Foo which has field bar. The bar field should be unique,
I have model Article it has field title with some text that may contain
I have the following contrived example in Rails. I want to make sure the
I want to make sure I have the appropriate associations here. Below I've listed
I have a system that relies on other models but want to make it
I want to make sure that I correctly understand the 'Effectively Immutable Objects' behavior
I have django model which consist of parent child relationship filed I want to
Let's say I have User model and Post model. Post model contains field user_id

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.