Blind Squirrel

Once in a while, even a blind squirrel finds a nut.

DRY Associations in Rails

I found a nice little article from a few months ago on The Rails Way. Written by koz, Assocation Proxies talks about some Best Practices for using assocations within Rails. I really felt the suggestions hit the proverbial nail on the head in terms of following the DRY principles. The first suggestion was on the best way to restrict access to user specific information. It’s incredibly useful to take advantage your RESTful patterns and make a single call like:

@todo_list = current_user.todo_lists.find(params[:id])

Of course, I also loved the final suggestion in Case Three: using a has_many with a condition on it:

has_many :completed_lists, :class_name => "TodoList", :conditions => { :completed => true }