Buck and Koz show put on a sesssion of do’s and don’t s at railsconf – an expression of the rails way vision – Helping us to clean up our code with smart coding practices
During the session they reinforced we should associations instead of finders to clean up our code.
…and while demonstrating they left us with a line that read something like “Blog.find_all_by_author_id” – and I thought to myself. No, they must be wrong. Don’t associations have the same sugar? [I can’t remember the exact code, but a few of us thought the use of the syntax wasn’t quite free…]
Can’t I do Blog.find_all_by_author(author) ?
Banging on our laptops in the lunch space, it seems they don’t exist.
Not for long. This is railsconf.
FindByAssociation gives Active Record dynamic finders for associations. Find_by_association helps us by determining the join conditions, :includes required tables, and builds a where clause for our needs.
Post.find(:all, :conditions => [“tags.id = ?”, tag.id], :include => :tags)
#After. ahh. joy
The nNovation Group inc.