What happens when I chain queries?

I have noticed that it’s possible to write queries like this:

DB.customer_wells.where('customer = ?', view.selected_customer.name).where('regions = ?', view.selected_region)

What happens in this case?

The AND operator is automatically applied to chained queries.

In other words, this:

DB.customer_wells.where('customer = ?', view.selected_customer.name).where('regions = ?', view.selected_region)

is equivalent to this:

DB.customer_wells.where('customer = ?' and regions = ?', view.selected_customer.name, view.selected_region)

Bonus item:

You can also pass in an object as a filter like this:

var filters = {region: view.selected_region};
if (view.selected_customer != undefined) { filters.customers = view.selected_customer }
view.well = DB.customer_wells.where(filters)

This only works if the query operators are = (doesn’t work for !=, <, etc)

1 Like