rails plugin: find_by_association


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


write up: http://www.nnovation.ca/2007/6/4/rails-plugin-find_by_association



General Partner

The nNovation Group inc.


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


-- fxn


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.


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