N+1 queries on Rails basics
Just enough fundamentals to be fluent preloading associations with ActiveRecord, and start helping your team to avoid n+1 queries on production.
Read moreJust enough fundamentals to be fluent preloading associations with ActiveRecord, and start helping your team to avoid n+1 queries on production.
Read moreLearn different ways to solve n+1 queries beyond "includes", to be confident that you can handle tasks on complex data models and fix n+1 queries if they appear.
Read moreI have some posts about using lateral joins in rails to fetch the “top N” of each record. Some months ago Ben Sheldon helped me see a performance problem on the queries that I was using.
Here I try to explain the problem, the solution proposed by...
Read moreYesterday, I found myself struggling with “litesearch”, and in my frustration, I decided to create a simple “Searchable” module to perform a “LIKE” search but with a cleaner interface/API.
If you’re looking for a straightforward way to implement a...
Read moreI have already shared a way of implementing a “read-more” behavior truncating by the number of lines instead of the number of words.
But now I want to share how you can do it using the line-clamp
css property.
I was reading about shoes.rb and, while scanning the book and walkthrough, I discovered a pattern that caught my attention for its power and simplicity.
The combination of “stacks” and “flows” allows you to build complex layouts, like the following...
Read moreN+1 queries are not always a problem, but I have seen that most of the n+1 queries that are really a problem are when we need to fetch data to compute something.
Here I will try to share some examples of posible expensive computations candidates to...
Read moreThere are many tools that can help you detect n+1 queries in different ways.
This is a little reference of some of those tools:
You don’t need to use all of them, but is good to know that they exists and how they can help you.
Imagine that you want to truncate a filename, but you want to keep showing the extension of the file. Like “A big file name that…awesome.pdf”. How would you do it?
Imagine that you are building a custom CMS. Within the form to edit an Article
, you need to have two buttons: a normal “Save” button and a new “Save and publish” button. And maybe, additionally, you will need a third button to delete the article.
Imagine that you have a form to update a record (let’s say a product record) and inside the form, you are showing a list of images, and each image needs a button to remove it. You tried to use button_to but it doesn’t work because in html you can have...
Read moreIf you want to add images to a record but you don’t want to use a JavaScript plugin or write any custom JavaScript, you can use a regular file field, Active Storage, and vanilla Rails.
Here I try 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 posts.