-
Finding Nemo
- robert
- July 10th, 2008
- 08:50:19 PM
I have been busy this week cleaning up a task that is a major part of our project’s Phase Two. (The deadline is coming up next week!)
I cannot go into to much information about the task I have been working on, but it involves a lot of automation to get things done easily (IE: Think millions and millions of robots doing my evil bidding. And yes, one of them will even go to the fridge to grab a soda for me.)
After completing the admin pages for these automated tasks, I was feeling quite accomplished. Then Rails decided to take all my feelings of accomplishment and throw them right out the window.
Instead of having a nice relaxing day of coding the publicly viewable page, it turned into one nightmare after another! I had written a pretty beefy SQL query that formats everything I want nicely, using a Select and Group By clause. My only problem with it… was the very reason I liked it so much: it broke Eager Loading!
Without eager loading, it was not an acceptable solution (to me). So, I decided to approach the problem differently. Instead of using a Find and doing a Group By, I created a has_many relationship in my model with an enforced limit.
This worked all great and dandy, but when it eager loaded the has_many relation, it was a bit too eager and loaded all the records in the database.
At first you might think… “Aww, how nice and helpful this little guy is… It wants to eager load EVERYTHING.”
Well, that would be the WRONG way to think! I set a limit of 5 records, but it was loading a good few 1,000 records. Just a giant waist of time and space.
Needless to say, this was a failed relationship and I quickly broke it off before it got to serious. (That’s how I roll.)
In my third, and final attempt (third times the charm!), I decided to write a method that would take care of everything for me.
In the end, it required more code and time, but this new method worked perfectly… and I am happy with the solution.
Wewt!
Comments
Poop?
BrightMix sees Dark Knight @ IMAX
Robert's victory pose
Trick Starbursts
[...] bookmarks tagged finding nemo Finding Nemo saved by 5 others Sarumy bookmarked on 07/15/08 | [...]