Posts

Showing posts with the label ActiveRecord Tips

Optimizing Database Queries in Ruby on Rails: Best Practices and Examples - Part 2

Hi, This is  Shubham Mishra  from India,  this is the part of  Ruby on Rails  exploration journey.  In this post we will discuss about the ruby's some special concepts and I am sure you will get excited to make your hands dirty with those code, so why to wait let's get started... Before jumping over different code snippets lets read some theory first,here are some tips for optimizing Ruby on Rails ActiveRecord queries: Use eager loading: As discussed earlier, eager loading can be used to fetch associated records in a single query, rather than making a separate query for each association. This can greatly reduce the number of database queries and improve performance. Avoid N+1 queries: N+1 queries occur when a query is made to fetch a record, and then additional queries are made for each associated record. This can be avoided by using eager loading, or by preloading associated records using the includes method. Use appropriate indexing: In...