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 their fab rails way vision – Helping us to clean up our code with smart coding practices. (Buck and Koz show)

During one session they reinforced we should associations instead of finders to clean up our code.

..and while demonstrating the 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.

And so is the intro, that started with chatting with Chris O’Meara @ Code Rhythm(and Chris’ bud) and ended with me pounding keys at PDX and JFK while Tibet Sprague of Public Display was knocking off the code for a new startup.

FindByAssociation gives Active Record dynamic finders for associations. This plugin doesn’t provide new functionality – but new sugar.

Dynamic finders currently exist for attributes (find_by_title, find_all_by_tag_id, etc), but not for associations. Find_by_association helps us by determining the join conditions, :includes required tables, and builds a where clause for our needs.

1
2
3
4
5
6
7

 #Before
  Post.find(:all, :conditions => ["tags.id = ?", tag.id], :include => :tags)

 #After. ahh. joy
  Post.find_all_by_tag(tag)

As with atttribute finders, with find_by_association you can find_all or find_ (returns first), and you can chain associations ala Post.find_all_by_author_and_tag(author, tag). You can also use instance or id for the param (author, or author.id). Find_by_association works with has_many (and has_many :through), belongs_to_, has_one and has_and_belongs_to_many associations.

Notes:

  • Tests are run via the rails AR procedure (read RUNNING_UNIT_TESTS). Currently contains tests only for mysql. Mysql tests run via “rake test_mysql” (after db and tables created) tests currently include AR Finder tests from Rails 1.2.3 (AR 1.15.3). Testing requires that rails is vendored. Not tested with edge. [TODO]
  • Does not mix with find_by attributes (can’t do Post.find_by_title_and_tag(‘post title’, tag)). So use either on or the other – mixing will not provide expected results. [TODO]
  • Class level finders – no instance finders [TODO]

Acquire via subversion at:

http://svn.nnovation.ca/svn/find_by_association/trunk

script/plugin install http://svn.nnovation.ca/svn/find_by_association/trunk

Gotta a comment?

5 Responses to “rails plugin: find_by_association”

  1. Brendan Baldwin Says:

    I recently released a plugin called findby_association that *does* mix with attributes and even lets you find through nested associations and those associations’ associations and/or attributes. Check it out if you’re interested in some active recordy goodness @ http://www.usergenic.com/projects/ruby/rails/plugins/findby_association

  2. Jim Says:

    Post.findallbytagid already exists, but doing @tag.posts is much simpler and doesn’t need a plugin

  3. Rstevens Says:

    I agree that doing @tag.posts is much simpler

  4. stinky Says:

    Thank u!!!

  5. Rstevens Says:

    No Probs !!

Leave a comment