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.
Although there are tools that can help you detect n+1 queries before they hit production, I think that is good to be able to identify n+1 queries directly by watching your logs…
Not all the tools will tell you exactly that you have n+1 queries, and...Read more
Maybe you already know that “fixing” an n+1 queries problem can hurt the performance of your app…
But even if you already know it and also understand how it can happen, it could not be obvious how to know if this is will happen with your fix.
I want to share with you an example application to help you visualize why “fixing” an n+1 queries problem, can hurt the performance of your application if you preload assocations with too many records.
It simulates the index page of a blog application...Read more
Sometimes people say that “fixing” some n+1 queries could hurt performance…
Maybe this phrase can be confusing, because if you have not been exposed to a lot of n+1 queries problems, it could be hard to imagine how it can be possible.
And also is...Read more
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?