published under license Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)copy! share!
posted in category Software Development & Programming / Rails ActiveRecord
posted at 05. Dec '20
Howto Preload acts_as_taggable
model Article
acts_as_taggable
acts_as_taggable_on :management
end
These commands avoid n+1 queries:
Article.includes(:tags => :taggings).map{|article| article.tags}
do not use #tags_list
or #management_list
, but map the tags:
Article.includes(:tags => :taggings).map{|article| article.tags.map(&:name)}
Preload different context on Article
class called “management”:
Article.includes(:management => :taggings).map{|article| article.management.map(&:name)}
Add Comment