Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Booking validation #3

Open
juhat opened this issue Mar 29, 2018 · 0 comments
Open

Booking validation #3

juhat opened this issue Mar 29, 2018 · 0 comments

Comments

@juhat
Copy link

juhat commented Mar 29, 2018

Booking.all.each do |b|

Booking validation is not really nice. It however works.

The main problem here: you load all the records from database to memory by calling Booking.all. It just does not work with more records. Let's say you have 10.000 bookings. This will fail. It is much better to write custom validator function which sends only one db query.

validate :check_dates_for_me_please

def check_dates_for_me_please
  existing_booking = Booking.find_by(room_id: self.room_id, start: ..., end: ...)
      errors.add(:room_id, "your record matches other record")  if existing_booking
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant