Greetings,
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
http://onrails.org/articles/2007/05/20/railsconf-2007-day-4
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.
#Before
Post.find(:all, :conditions => [“tags.id = ?”, tag.id], :include
=> :tags)
#After. ahh. joy
Post.find_all_by_tag(tag)
write up: http://www.nnovation.ca/2007/6/4/rails-plugin-
find_by_association
Cheers,
Jodi
General Partner
The nNovation Group inc.
www.nnovation.ca/blog