It was very weird. It would reliably crash the server process. I didn't investigate any further - I just resorted to something else.

I've actually removed the lines that do the special case for Legacy SFLG loans (due to a conversation with Linda). This shortens it to the following, which is a little nicer (e7013bf1032af873fede2afb96a90b0f200645ba).

      type_condition = ->(type) { "(loans.loan_scheme = '#{type.scheme}' AND loans.loan_source = '#{type.source}')" }
      scope = scope.where(loan_types.map(&type_condition).join(' OR '))