Solve the N + 1 problem with Ruby on Rails: acts-as-taggable-on

environment

Rails.version
=> "5.2.4.1"
ActsAsTaggableOn::VERSION
=> "6.5.0"

model


class Model < ApplicationRecord
  acts_as_taggable
end

solution

To eliminate N + 1, use includes (: tags) and then call [tags] 1 instead of [tag_list].

#Calling tags
Model.includes(:tags).each{|m| m.tags }

# acts_as_When using taggable skills etc.
Model.includes(:tags).each{|m| m.skills }

# tag_If you call list, N+1 will inevitably occur
# Model.includes(:tags).each{|m| m.tag_list }

# tag_To be the same as list...
Model.includes(:tags).each{|m| m.tags.map(&:name) }

Recommended Posts

Solve the N + 1 problem with Ruby on Rails: acts-as-taggable-on
Publish the app made with ruby on rails
[Competition Pro] Solve the knapsack problem with Ruby
Determine the current page with Ruby on Rails
[At Coder] Solve the ABC183 D problem with Ruby
[At Coder] Solve the ABC182 D problem with Ruby
Programming with ruby (on the way)
[Note] About the Fizz_Buzz problem (How Ruby on Rails works)
I tried to solve the problem of "multi-stage selection" with Ruby
[Ruby on Rails] View test with RSpec
Understand code coverage with Rspec, the Ruby on Rails test framework
part of the syntax of ruby ​​on rails
Notes on using FCM with Ruby on Rails
[Ruby on Rails] Controller test with RSpec
[Ruby on Rails] Model test with RSpec
[Beginner] Let's solve AtCoder problem with Ruby while looking at the article!
I tried to solve the tribonacci sequence problem in Ruby, with recursion.
How to solve the local environment construction of Ruby on Rails (MAC)!
[Ruby on Rails] Until the introduction of RSpec
Introducing Rspec with Ruby on Rails x Docker
When the Ruby on Rails terminal rolls back
[Rails] Procedure for linking databases with Ruby On Rails
[Ruby on Rails] Upload multiple images with refile
I made a portfolio with Ruby On Rails
Ruby on Rails Elementary
Ruby on Rails basics
Ruby On Rails Association
[Ruby on Rails Tutorial] Error in the test in Chapter 3
Docker the development environment of Ruby on Rails project
[Ruby on Rails] Delete s3 images with Active Strage
Run Ruby on Rails RSpec tests with GitHub Actions
[Ruby on Rails] How to change the column name
Challenge the settings for developing with vue.js on Rails 6
Solve the Discrete Logarithm Problem with an arbitrary mod
(Ruby on Rails6) Reflecting the posted content from the form
Created RSS / Atom format sitemap with Ruby on Rails
Try using the query attribute of Ruby on Rails
[Ruby On Rails] How to use simple_format to display the entered text with line breaks
[Beginner's point of view] I tried to solve the FizzBuzz problem "easily" with Ruby!
Solve Google problems with Ruby
Portfolio creation Ruby on Rails
Don't underestimate the N + 1 problem!
Ruby on rails learning record -2020.10.04
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05
Ruby on Rails basic learning ①
[Ruby on Rails] about has_secure_password
Ruby on rails learning record-2020.10.07 ②
Commentary on partial! --Ruby on Rails
Let's solve the FizzBuzz problem!
Ruby on rails learning record-2020.10.07 ①
Cancel Ruby on Rails migration
[Ruby] problem with if statement
Ruby on rails learning record -2020.10.06
Ruby on Rails validation summary
Ruby on Rails Basic Memorandum
[Ruby on Rails] Only the user who posted can edit
I tried installing Ruby on Rails related plugin with vim-plug
[Ruby on Rails] Add a column with a foreign key constraint
[Ruby on Rails] Quickly display the page title in the browser
(Ruby on Rails6) Display of the database that got the id of the database