I am building a rails app which has the models Users
, Album
(as in photo album) and Photo
, nested in that order. I want to be able to find the total storage space used by a user's albums.
From the Album
level, I can find the total storage space used by the photos in that album. i.e:
album = Album.first
album.photos.sum(:image_file_size)
But, I want to be able to do this from the User
level for all albums' photos.
Is there an elegant way of doing this?
Thanks!
Assuming that each User
has many Albums
and each Album
has many Photos
. You may get the total storage used for the first User
using the below statement.
User.find(1).albums.map(&:photos).flatten.pluck(:image_file_size).sum