I came across some weird behavior yesterday in a Rails app where we were querying records where a polymorphic relationship excluded a particular instance.
Specifically, we have a model for transactions, which have a related source, which can either be an Order or a CreditSource, and we wanted to find all transactions by a user that didn’t not have a specific order instance as its source.
Easy, right?
Turns out ActiveQuery interprets that in a not so useful way:
This means that Active Query was fetching all of the user’s transactions where the source type was not order and the source ID was not 1, ie all credit sources that did not have ID 1, ie, meaningless.
What we really wanted was something more like this:
Or, alternatively:
This means find all of the user’s transactions where the source type is a credit source, or, if it’s not a credit source (ie is an order), then exclude cases where its ID is 1.