Rails Rumble Retrospective
I had a fantastic time building an application in 48 hours for Rails Rumble. I built ShareLocally, a site that helps people share things with their neighbors. Instead of promoting the app (which I still think is totally awesome and plan to keep hacking at), I’m going to examine some of the things I learned from my first Rumble.
Read On »View CommentsInstead of Rails :unique finder, Array#singular
There was recently some talk about a suggested :unique finder for rails.
Although this seems like overkill, I occasionally do things like array.size == 1 && array.first, which is basically all the :unique finder would do.
Without further ado, here’s a five-line monkeypatch to Array that lets you get the singular item from an array (or blow up if you expect one and there isn’t):
class Array def singular?() size == 1 end def singular() singular?? first : nil end def singular!() singular or raise "not singular" end end
Now you can simulate the :unique finder just by doing
my_singular_wibble = Widget.wibbles.first.singular.
This, along with lots of other fun monkeypatches, is now in freighthopper, my collection of extensions and monkeypatches.
View CommentsPromiscuous models and capable controllers
By default ActiveRecord allows visitors access to any writer method, that is, any method ending with an equal sign. This comes courtesy of the ActiveRecord::Base#attributes= method, which is used internally by the main methods that handle creating and updating records, including new(), create(), and update_attributes().
This topic recently came up on the SF and East Bay Ruby meetup lists (original message here, associated blog post here). I started writing a response in my email client and decided it was big enough that it would benefit from a blog post.
Abstract: Even though attr_protected and attr_accessible exist, models
shouldn’t be filtering — controllers should be. Filtered params should be defined on the model for locality of reference, not on the controller. Hash#slice and Hash#except aren’t enough because they don’t handle nested hashes, so I explore a recursive filter (blacklist & whitelist).
Delayed::Job send_later with :run_at
I just discovered Delayed::Job’s run_at column and I’m quite happy with it. I hadn’t realized that DJ comes with cron-like scheduling right out of the box, and at stormweight, we’ve been running some DJ-like async tasks with cron/rake because of this (checking email every minute on the minute, for example). One downside of this was that memory usage was inconsistent because every time cron performed a task, rake fired up another rails instance with a 70-100mb memory footprint, even if the DJ workers were sitting there with no work to do.
Read On »View CommentsDelayed Job and Monit
When I first got started running Delayed::Job under Monit, I searched around for a pid dropping launch script but couldn’t find one.
Read On »View Comments