Under the hood: ActiveRecord::Base.find, Part 1

Posted by Jamis on November 17, 2006 @ 03:55 PM

People attribute a lot of magic behavior to ActiveRecord::Base.find, and rightly so. It does a lot of different things. Want to find a record by id? Done. Multiple records by id? Done. Records based on custom conditions, including associations (arbitrarily deep), and limit the result? Done, done, and done.

However, all this magic comes at a price: the implementation of find is non-trivial.

I’d like to help people better understand how find works. By understanding what it does and how it does it, you’ll be able to make better decisions about your own applications, such as when eager loading of associations might be a bad idea, or what kinds of indexes your tables need.

So, next week (Thanksgiving notwithstanding) I’ll work on getting the first installment out. I learned one very important thing from the last “Under the hood” series I did: don’t write monstrously long articles, because most people won’t read them. So, this series will consist of much shorter, more bite-sized looks at specific parts of the #find implementation. Stay tuned!

Posted in Under the Hood

Comments

Have something to add? Click here to leave a comment.

17 Nov 2006

1. Brad Ediger said...

You mean the class method, right? ActiveRecord::Base.find() ?

Keep ‘em coming, Jamis. Loved your routing series.

2. Jamis said...

Gah, indeed I did mean the class method. Thanks for keeping me honest, Brad! (Fixed.)

3. np said...

Routes were great and I can’t wait for find() !

Thank you so much for this unvaluable humanized-expert-documenting effort, Jamis!

I started programming (only did some xhtml/css) about six months ago with ruby and rails (or was it rails and ruby) and these kind of in depth and easy to follow explanations helps me greatly.

4. Tom said...

Especially looking forward to better understanding the relationship with indexes!

5. Amr said...

This is so awesome! thanks for all your hard work and effort to make people understand.

18 Nov 2006

6. Hendy Irawan said...

Yes, I agree!

I’m referring to “bite-sized”.

I have learned that this is very true, for almost any kind of printed/textual material.

I’ve been trying to write shorter and shorter articles, but I’ve failed most of the time. Max-300 words (how long is that?) or 5 paragraphs is very unattainable for me. Weird.

Even in comments!! ;-)

7. John Topley said...

Wow Jamis, it seems you’re on fire with your articles here and The Rails Way. We’re all learning lots from them, so long may it continue!

19 Nov 2006

8. Sur said...

Thanks for the onblog rails tutorials Jamis. We all are learning gr8 things with your gr8 posts.

20 Nov 2006

9. Sean Cribbs said...

I’d love to see this, especially since I spent a lot of time this weekend writing some hefty dynamic query constructors. Of course, mine had to deal with joining multiple tables, but the beauty of ActiveRecord::Base.find() is how succinct you can make queries…ooh the suspense.