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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:08:43+00:00 2026-06-07T08:08:43+00:00

I have a model, Clients and a corresponding database with lastname and firstname columns.

  • 0

I have a model, Clients and a corresponding database with lastname and firstname columns. Originally there were no constraints on the uniqueness of [lastname, firstname], and the database currently contains duplicates. I would like to clean up the database and impose constraints on the model, such as: validates_uniqueness_of :lastname, scope: :firstname.

Idea that comes to my mind is to back up the data in some fashion, impose constraints on an empty model database and then pull the data back in with duplicates that I can now process separately rescueing from exception.

I feel, however, that I am doing something off-the-wall here.

Is there a better, “rails way” to do this?

  • 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-07T08:08:46+00:00Added an answer on June 7, 2026 at 8:08 am

    The only really pure-Rails way to discover the problems is to run through every model and ensure that it is still valid. For instance, roughly:

    Client.all.each do |client|
      unless (client.valid?)
        puts "Client #{client.id} invalid: #{client.errors.full_messages}"
      end
    end
    

    Loading all records might be a bad idea if it requires too much memory. ActiveRecord 3.0 is supposed to be smarter about this, loading it in chunks, but I can’t prove this is the case at the moment.

    As for what you do with the duplicate data:

    • Always back up your tables before starting using the appropriate database snapshotting tool.
    • Always test your modifications on a copy of the data before running this on a production database.
    • Always document your changes by writing a Rails migration that performs the operation in a reliable and predicable manner. Test it repeatedly before deploying.

    I would presume that your production database is subjected to regular snapshots as it is, in which case you can grab test data from there. If this is not the case, your first priority should be to ensure it is.

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

Sidebar

Related Questions

I currently have a client that wants me to 'abstract' out a domain model
I have a project in which I have a database model class provided along
A basic overview of my app. There is currently two models. A jobs model
I have a core data model with an entity called clients that is made
I have a SelectList in my MVC2 model. In the case that there's only
I have a model that collects financial data from my clients each calendar quarter
We currently have developed an application using WCF. Our clients make connections to different
I have a Client model with a OneToOne relationship to User, to extend the
I have HABTM models Client and Book . Client model has a bookshelf_color attribute
I have a model with many fields (nearly 40). The client wants the fields

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.