Home

Feb 28

List pending production migrations in Rails

Typically, you’ll know what migrations need to be run on the production database on deploy. You can check on a production server with rake db:migrate:status | grep down, but that requires the server to have been deployed with the migrations that need to be run. It’s much more convenient to be able to see what migrations in your local project have yet to be run on the production database.

Logger output

Feb 21

Rails Mixin: Skip automatic params parsing

There’s a common pattern when receiving third-party notifications (a.k.a. webhooks) in Rails applications: using request.raw_post to initialize a wrapper around the notification data. ActionDispatch automatically parses the body of the request into the request.params hash, which can incur significant overhead for large payloads. Skipping this automatic params parsing can result in a big speedup:

New Relic Stats

Feb 14

Rails Mixin: Log which line caused a query

Rails helpfully logs queries to ActiveRecord::Base.logger in development mode. This logging is an easy way to keep an eye on unexpected costs hidden by Rails’ abstractions - like N + 1 queries - which might otherwise slip by unnoticed. One thing I’ve found that makes this query logging even more useful is to log the line of code that caused the query.

Logger output

Jan 31

Rails Mixin: Auto-instantiated Associations

Often times you have an association that you want to be available as soon as you access it. That is, given a User model that has_one :location, you want to call user.location.country without worrying about whether user.location has been instantiated.

Dec 08

Automatic Code Reloading in Rails Console

Isn’t it nice how Rails server automatically reloads your code before each request in the development? Wouldn’t it be nice if Rails console worked the same way? Sure, you can manually call reload!, but gems like active_reload - or Rails 3.2 itself - make reloading a near no-op most of the time, so we really don’t have to be so stingy about reloading.

Here’s how you can get IRB to reload your code automatically.