Hi, my opinion is that calculations of this kind are always messy no matter how much icing you heap on top of it. I try to avoid them in web apps as much as possible.
If your user base is really small...maybe an internal office app, then I guess it's ok to do things this way. However, if you have thousands of users whom in turn have thousands of attachments and galleries, your application will suffer 'bottle-necking' when this action is called.
I would take a different approach here by adding a total_bytes column to your users(?) table. Then I would employ an after_create/before_destroy in your attachment model to update the total_bytes field. You could do the same with your gallery/page models(?). So now all you have to do is call @user.total_bytes and you get your value without having to rely on repetitive calculations.
On the other hand, if you need to perform an administrative action that gets the total disk usage for all your users, then you could use a calculation to figure it out. Hope this was somewhat helpful and check out my blog post for more on this type of thing.
Last edited by RailsRocks (2009-01-01 12:19:08)
It's not the size of the app that matters, it's how you code it. ~ railsenvy