I have a Part model that has_many :images, through: :gallery and I would like to implement counter_cache to store the images count of a part. This is my setup.
Part
has_one :gallery, dependent: :destroy
has_many :images, through: :gallery
Gallery
belongs_to :part
has_many :images
Image
belongs_to :gallery
I can do @part.images to fetch images of a part and now I would like to cache images count so I can do @part.images.size or even order parts by images_count. I would normally do this using counter_cache: true on a belongs_to side but how do I do this in this case? Is it possible?