Skip to content Skip to sidebar Skip to footer

Rails Basics

Rails Basics

How to Solve N+1 Query Issues in Ruby

query Issues in Ruby If you want to solve N+1 query issues in Ruby, you need to use the ActiveRecord#eager_load or ActiveRecord#includes methods. # without eager loading posts = Post.all posts.each do |post| puts post.user.name end # with eager loading posts = Post.includes(:user) posts.each do |post| puts post.user.name end The ActiveRecord#includes method will eagerly load the associations (in this case, the user association).…

Read more

Clean your views with Rails Helpers

Rails Helpers Rails helpers are methods defined in your controller or view files. They are used to format data before displaying it to the user or to create dynamic HTML elements. You can create your own helpers throughout your application or use the built-in helpers provided by Rails. Using Rails Helpers To use a helper method, you can call…

Read more

Guide To Implement Rails Associations

Rails associations are a way to link one model to another. There are four different types of associations: One-to-one One-to-many Many-to-many Polymorphic Each type of association has its own set of methods that you can use to interact with the associated data. One-to-One Associations A one-to-one association means that each record in one…

Read more

How To Implement Cache In Rails

To implement a cache in Rails, you can use the ActiveSupport::Cache class. The ActiveSupport::Cache class provides a unified API for a variety of caching backends. For example: Memory store File store Dalli store You can use the ActiveSupport::Cache::Store#fetch method to read from the cache. If the data is not in the cache, the block you pass…

Read more