Making things work isn't enough for you any more? Now you need to consider performance and scalability?
Do you hope to some day come with efficient solutions by yourself rather than stumble on many stackoverflow questions to get something done?
Do you feel unqualified to tackle complex tasks with complex data models?
Do you feel that you need to sharpen your sql/active record concepts?
Maybe I can help you!
I send an email each week, trying to share knowledge and fixes to common problems and struggles for ruby on rails developers, like How to fetch the latest-N-of-each record or How to test that an specific mail was sent or a Capybara cheatsheet. You can see more examples on Most recent posts or All post by topic.
I want to tell you that I have added a new section to the guide for preloading associations in rails, to introduce you to something I call “Preload objects” that will help you build complex preloads for those cases when you can’t find a way to do what you need to do, with the standard rails mechanisms.
Sometimes just using
includes is not possible… or maybe it is, but you just can’t figure it out how to do it.
Maybe you want to preload some records matching two keys, or preload a grouped relation.
Maybe you know how to represent the association with a
:through, but you need something faster, or to use less memory.
In that kind of situations, one thing that you can do is to write a custom object to represent that preload.
Here I will try to explain how and when you can do it with an example.
Maybe you are already familiar with
preload, but you know
that a lot of the time you will need more than just
It is common to have complex nested associations, scopes that you need or want to reuse, places where preloading all the associated records could hurt the performance of your app.
How do you work with complex nested associations?… How do you simplify the preloading of those nested associations?… How do you “preload an scope”?… How do you preload just the latest n of each record?
A common cause of n+1 queries is fetching the “latest-N-of-each” record on a list of records.
Some examples of this problem are trying to get…