Rails Postgres Query Fails to Return New Records

Rails Postgres Query Fails to Return New Records

This Rails code is supposed to prevent duplicate records from being recorded by the server within 20 seconds:

This Rails code is supposed to prevent duplicate records from being recorded by the server within 20 seconds:

@transit = Transit.new(tag: params[:tag])
if Transit.where(tag: @transit.tag).where("created_at > ?)", 20.seconds.ago).first
  logger.warn "Duplicate tag"
else
  @transit.save!
end

However, this is not working. I can see in my production database (hosted on Heroku) two different records getting created with the same tag 10 seconds apart.

Logs show the correct query is executed on the second request, but it returns no results and saves a new record anyway.

Why does this happen? I thought Postgres' default isolation level of read_committed would prevent this from happening. The query that returns no records should miss Rails' SQL cache. Logs show both requests were handled by the same WEB.1 Dyno on Heroku, and my Puma.rb is set up for 4 workers and 5 threads.

What am I missing?

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

How To Use PostgreSQL with Your Ruby on Rails Application on macOS

In this tutorial, you will set up a Ruby on Rails development environment connected to a PostgreSQL database on a local macOS machine. You will install and configure PostgreSQL, and then test your setup by creating a Rails application that uses PostgreSQL as its database server.