String matching in Postgres

Postgres is a pretty powerful tool, and it offers a slew of useful features beyond mere data persistence and query execution. One such useful feature is string matching the capabilities of which are available both as extension e.g. fuzzy string matching. You can for instance pass a regular expression, that a particular model attribute is to be matched against directly to the database query, for instance if I want all users of a certain email domain to assign them to a newly crea...

Rails/ Postgres: WHERE clause matching multiple columns

Say we want to filter a table matching pairs of values. E.g. For each account we want to select all items from the most recent transaction. First we group most recent transactions by account id, then we filter all items the associated transaction has by processed_at and account_id in the array of pairs we retrieved in the first query.

tuples = Transaction.where.group(:acount_id).pluck('account_id, max(processed_at)')

Item.joins(:transaction).where("(transactions.account_id, transactions.processed_at) IN (VALUES #{tuples.map { '(? ,...

Ruby: Avoid assigning return value of methods to local variables of the same name

From the "Programming Ruby Book":

Assignment to a variable works as you would expect: the variable is simply set to the specified value. The only wrinkle has to do with variable declaration and an ambiguity between local variable names and method names. Ruby has no syntax to explicitly declare a variable: variables simply come into existence when they are assigned. Also, local variable names and method names look the same—there is no prefix like $ to d...