rails plugin: find_by_association

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

ranting o the technology and business of software

Some months ago I saw a plugin that provides that feature, I think it was this one

   http://agilewebdevelopment.com/plugins/dynamic_finders_with_belongs_to_names

-- fxn

harump!

In addition to belongs_to, find_by_assocations works will all other association types.

Really cleans up references to join tables in has_many, and has_many :thtorugh's (and has_and_belongs_to_many).

thanx for the ref Xavier.

Jodi

Awesome! Thanks, this is a feature that I have wished for many times.
nathan