Under the hood: ActiveRecord::Base.find, Part 1
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!
Reader Comments
You mean the class method, right? ActiveRecord::Base.find() ?
Keep ‘em coming, Jamis. Loved your routing series.
17 Nov 2006
Gah, indeed I did mean the class method. Thanks for keeping me honest, Brad! (Fixed.)
17 Nov 2006
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.
17 Nov 2006
Especially looking forward to better understanding the relationship with indexes!
17 Nov 2006
This is so awesome! thanks for all your hard work and effort to make people understand.
17 Nov 2006
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!! ;-)
18 Nov 2006
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!
18 Nov 2006
Thanks for the onblog rails tutorials Jamis. We all are learning gr8 things with your gr8 posts.
19 Nov 2006
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.
20 Nov 2006