Today I Learned

TIL, 2017-07-07

  • Mental health thing: “It’s 2017. We are in a knowledge economy. Our jobs require us to execute at peak mental performance. When an athlete is injured they sit on the bench and recover. Let’s get rid of the idea that somehow the brain is different.”
  • When the database catches such a duplicate insertion, ActiveRecord::Base#save will raise an ActiveRecord::StatementInvalid exception. You can either choose to let this error propagate (which will result in the default Rails exception page being shown), or you can catch it and restart the transaction (e.g. by telling the user that the title already exists, and asking them to re-enter the title). This technique is also known as optimistic concurrency control.
  • AR::Base.update_all: Updates all records, but constructs a single SQL update statement and sends it straight to the database.
  • AR::Base#assign_attributes then AR::Base#save to skip validations, then check after (this will allow swapping). assign_attributes is like book.title = 'Hello'; book.color = 'red'.
  • If you rescue the AR::Rollback, then the database transaction will not rollback. What you do inside an AR::Transaction would be to raise any other error, and that will cause the transaction to fail. Reference
  • Warning: one should not catch ActiveRecord::StatementInvalid exceptions inside a transaction block. ActiveRecord::StatementInvalid exceptions indicate that an error occurred at the database level, for example when a unique constraint is violated.
  • GuardClause, I think I’m getting it now. You just do early return as opposed to if x.valid?.

  • Apache Bench (ab).
  • siege.
  • wrk can be scripted with Lua.
  • find_each
  • ruby-prof. -m option. --printer.
  • rails runner
  • bin/rails is the symlink.
  • Rails startup is slow because of Rubygems and Bundler?
  • is used to determine that it is a Rack app.
  • rake middleware
  • Rails slimming down – remove middleware, ActionController::Metal.
  • Controllers are Rack apps.
  • Check test suites for Rails
  • 30 mb lighter RAM for slim Rails.
  • HTML minification on Cloudflare.
  • HTML can’t be cached because CSRF token changes every time you reload the page.
  • Chrome net-internals to check how long the SSL takes.

This project is maintained by daryllxd