1

How can I limit the amount of child records that a parent record can have?

A lot of the existing answers (for example: Limit number of objects in has_many association) seem to not consider potential race conditions (such as hurikhan77's answer) and try to implement the check in application-level code via a validation.

Would a table-level lock (EXCLUSIVE) be the correct solution or am I overlooking something?

Edward
  • 141
  • 5
  • 18
  • 1
    You could do it [inside the database](https://stackoverflow.com/q/1743439/479863) in [various ways](https://dba.stackexchange.com/a/110581/5359) and then handle the exception in Rails. A bit of [googling on constraints](https://www.google.ca/search?q=postgresql+constraint+number+of+rows) might be useful. – mu is too short Apr 04 '18 at 07:04

0 Answers0