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 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

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 1610
Alex Hales
  • 0
Alex HalesTeacher
Asked: May 30, 20222022-05-30T21:28:37+00:00 2022-05-30T21:28:37+00:00

ruby on rails – Prevent database duplicates in RubyOnRails

  • 0

[ad_1]

This is a SAAS solution with many customers.
We receiving a lot of reservations via Webpush for many different properties, when the reservation was created or changed on the other side.

Sometimes it happens, a new entry was created, and 30ms later, we receive an update.
Then our current approach fails sometimes. Currently we’re just using a simple lock to prevent changes at the same time. But the find_or_create_by!-Method before might be the problem which creates duplicates.

reservation = reservations.reload.find_or_create_by!(hotel_id: hotel.id, reservation_id: api_reservation.id, booking_id: api_reservation.booking_id)
reservation.with_lock { update_reservation(reservation, api_reservation) }

To prevent changes at the same time for one reservation, we just build a reservation with minimal data, which will be updated afterwards in the lock.
In the update, there will be some information added besides of the data from the creation. So the information from the initial creation are in the update als included.

To prevent duplicates, we added a validation in the model.
But there are some cases, where duplicates are okay, for example when there is no connection to a hotel system and the guest fills out the information by himself. So there are hundreds of reservations with the reservation_id “0000” oder “1234”,… because the guest doesn’t know the correct number often.

class Reservation
  validates :reservation_id, uniqueness: { scope: [:hotel_id, :booking_id] }, if: :to_be_uniq?
end

So there is currently no possibilty to validate this with a PostgreSQL Unique index.

With that ruby validation we had the strange problem, that we created the reservation and then the update failed, because the reservation already exists.
RuntimeError - ActiveRecord::RecordInvalid: Validation failed: Reservation has already been taken
When I checked the database, the error was a false positive.

We removed the validation because it caused more problems than it solved.

My last idea is to put random information into the reservations which are created manually. Then it would be possible to validate this via the unique index in PostgreSQL.

But I can’t understand, why an update with the same data, raises the Validation Error.
Does someone have any idea?

[ad_2]

  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 5256 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 1098 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 1047 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

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.