How to break Rails built-in Active Record's Validations

This past week I got an error which as it turn out originated because Rails failed with its built-in Active Record validation checkers.

This past week I got an error which as it turn out originated because Rails failed with its built-in Active Record validation checkers. 

I have this has_many association set up in my database - a `Website` has many `pages` 
Image

Each `page` has a unique URL scoped to a website. This means that pages under a website shouldn't have the same URLs.

I’m using Sidekiq workers to crawl a website in the background, get all it's on-page links and add those link as pages to the website. Sidekiq is multi-threaded - and runs thousands of jobs at the same time. Each of these jobs is creating a page with the crawled link as the URL. So, it may happen that two different jobs creates two pages with the same URL value at the same time, if it discovers two identical links on the website.

And this is exactly what happened.

Here are a few things I tried that didn’t stop Sidekiq from creating duplicate pages under the same website.
 
These didn't work.
Image

What worked instead was pretty simple.
Adding a unique index directly in the database solved this error.

This worked!
Image

And this works 100% of the time. Rails has documented this properly in their docs, and I fixed the error quickly once I knew what was causing it. But diagnosing this error initially to understand why this is happening took time. 

About the Author:
Arjun Rajkumar is a full stack developer and runs a web development company in Bangalore, India. He likes building digital products that your customers will love.


Weekly Newsletter

Subscribe to get my weekly newsletter and latest articles on startups, marketing and growth.

I’m Arjun Rajkumar, and I'll help you build a product that your customers will love.

Here is a sample of some of my past work.