I have a Category
model that has_many :items
. The items have a deleted boolean field that checks if the item has been deleted or not. When listing all the categories, I also want to print the count of deleted: false
items in that category
This is how I'm doing it
@categories = Category.includes(:items).all
When printing out the counts of not deleted items, I'm doing
category.items.get_all.count
get_all
is a scope inside item model
scope :get_all, where(deleted: false)
It's getting the job done, however I feel the page load time to be slower than other pages on my website. Is there any way I can optimize this code?