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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:58:51+00:00 2026-05-24T18:58:51+00:00

I have a model with an active column, which is a boolean. I want

  • 0

I have a model with an active column, which is a boolean. I want to validate the uniqueness of all newly added records against company_id, such that I can add as many records as I want with the same company_id to the table so long as active is set to false. There should only be one active record for each company_id.

How would I write this? I’ve already tried:

validates :company_id, :uniqueness => { :scope => :active }

But that seems to also validate against unique combinations of active being false (such that I can never have more than two company_id‘s in the table with the same active status, regardless of what active is)–the validation above allows two records for company_id, one with active = false and the other with active = true. Once those two records are in, the validation blocks everything else.

I then tried adding this:

scope :active, where(:active => true)

But that doesn’t seem to have changed the validation at all (same issue as above).

How can I write this validation so that I can add as many records with the same company_id so long as active is false, and only allowing one active = true per company_id?

  • 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-24T18:58:54+00:00Added an answer on May 24, 2026 at 6:58 pm

    No need to use validates_each – that’s only if you want to pass multiple attributes through the same block. Just create a custom validation:

    validate :company_id_when_active
    
    def company_id_when_active
      if active? and CompanyTerm.exists? ["company_id = ? AND active = 1 AND id != ?", company_id, id.to_i]
        errors.add( :company_id, 'already has an active term')
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have an Account model which has_many Users. Accounts have a boolean active
If I have Model.objects.all() I want to get only one object for any content_object=foo,
I'm using active admin for my rails app. I have a customer model which
I have a standard active record model with an attributes that is required: class
I have model Foo which has field bar. The bar field should be unique,
I have the following models: class Product(models.Model): active = models.BooleanField(default=True) name = models.CharField(max_length=40, unique=True)
I have a model, Domain , which has a text field, names . >
I have a minor mode. If that mode is active and the user hits
I have model Article it has field title with some text that may contain
Lets say I have model inheritance set up in the way defined below. class

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.