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...
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 { '(? ,...
Anatomy of an SQL query: 7 steps
- FROM generates the data set
- WHERE filters the generated data set
- GROUP BY aggregates the filtered data set
- HAVING filters the aggregated data set
- SELECT transforms the filtered, aggregated data set
- ORDER BY sorts the transformed data set
- LIMIT .. OFFSET frames the sorted data set
High Performance Browser Networking: HTTP/2
"The primary goals for HTTP/2 are to reduce latency by enabling full request and response multiplexing, minimize protocol overhead via efficient compression of HTTP header fields, and add support for request prioritization and server push. To implement these requirements, there is a large supporting cast of other protocol enhancements, such as new flow control, error handling, and upgrade mechanisms, but these are the most important features that every web developer should understand and leverage in their applications."
React: use component lifecycle methods for updates instead of event handlers
The core idea of React to turn the imperative management of data in an application into a declarative one. The best way to stick this philosophy is to consistently think about how to define behavior as function of props and state. Here is an example:
Say you have a time tracking component, to view the elapsed time and to switch to a different tracking; either resume and old one of start a new one (NEXT).(see below)
We would like for the current tracking to be automatically saved when we navigate to new or old tra...
Active Record: find_or_create_by with additional attributes
Say you want to fetch a record like a user with a particular nick name and if no user with the specified name exists, create one with certain additional attributes like email or full name.
The ActiveRecord query method find_or_create_by
takes care of finding or creating a record given an attribute, in order to create the record with the additional attributes without a second trip to the database, however, we can make use of create_with
like so:
attributes = {full_name: "Mr. Wombat", email: "womb@bat.com"}
User.create_with(attri...
Declare private accessors in Ruby
Sometimes you might want certain accessors to only be available within your class, so you make them private. However Ruby will yell a warning at you if you do something like this:
private
attr_accessor :name
#=> warning: private attribute?
In order to filter out this noise you can declare your own accessor like so:
module PrivateAccessors
def private_attr_accessor(*names)
attr_accessor *names
private *names
end
def private_attr_reader(*names)
attr_reader *names
private *names
end
def priva...