Reply to comment

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 }

Excellent!

Reply

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <bash>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <java5>, <javascript>, <lisp>, <php>, <python>, <rails>, <ruby>, <sql>. The supported tag styles are: <foo>, [foo].

More information about formatting options