Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4569620
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:14:43+00:00 2026-05-21T19:14:43+00:00

What I would like to happen is every single time a new file is

  • 0

What I would like to happen is every single time a new file is uploaded, user.space_used is updated.

But that doesn’t happen right now. In order to get an update, I have to manually run user.update_space at the console to update a specific user, or use an array to cycle through all the users and update it like that.

How do I get it to do it at the right time – also, it would be nice if I could verify that the space_used column on the User model has the total sum of the filesizes of all the files uploaded for that user on the Upload model.

My user model looks like this:

# == Schema Information
# Schema version: 20110412170916
#
# Table name: users
#
#  id                   :integer         not null, primary key
#  email                :string(255)
#  encrypted_password   :string(128)
#  password_salt        :string(255)
#  reset_password_token :string(255)
#  remember_token       :string(255)
#  remember_created_at  :datetime
#  sign_in_count        :integer
#  current_sign_in_at   :datetime
#  last_sign_in_at      :datetime
#  current_sign_in_ip   :string(255)
#  last_sign_in_ip      :string(255)
#  username             :string(255)
#  first_name           :string(255)
#  last_name            :string(255)
#  created_at           :datetime
#  updated_at           :datetime
#  invitation_token     :string(60)
#  invitation_sent_at   :datetime
#  plan_id              :integer
#  current_state        :string(255)
#  confirmation_token   :string(255)
#  confirmed_at         :datetime
#  confirmation_sent_at :datetime
#  space_used           :integer         default(0), not null
#  failed_attempts      :integer         default(0)
#  unlock_token         :string(255)
#  locked_at            :datetime
#  trial_end_date       :date
#  active_subscription  :boolean
#

class User < ActiveRecord::Base
    acts_as_voter
  devise :database_authenticatable, :confirmable, :registerable, :timeoutable,
         :recoverable, :rememberable, :trackable, :validatable, :invitable, :lockable

  attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :first_name, :last_name, :plan_id

    after_save  :update_space

  def role_symbols
    roles.map do |role|
      role.name.underscore.to_sym
    end
  end

  def update_space
       total_size = 0
         if self.uploads.count > 0
           self.uploads.each do |upload|
             total_size += upload[:image_file_size]
           end
         end
    self.space_used = total_size

  end

    def space_threshold_reached?
        self.plan.storage == self.space_used        
    end

    def space_left
        (self.plan.storage * 1024 * 1024 * 1024) - self.space_used.to_f     
    end


end

My Upload model looks like this:

# == Schema Information
# Schema version: 20110330215959
#
# Table name: uploads
#
#  id                 :integer         not null, primary key
#  name               :string(255)
#  description        :string(255)
#  image_file_name    :string(255)
#  image_content_type :string(255)
#  image_file_size    :integer
#  image_updated_at   :datetime
#  stage_id           :integer
#  user_id            :integer
#  created_at         :datetime
#  updated_at         :datetime

class Upload < ActiveRecord::Base
    acts_as_voteable
    has_attached_file :image, :styles => { 
                                                                :thumb => "64x64" },
                                                            :storage => :s3,
                                                            :path => "/:style/:id/:filename"

    validates_attachment_presence :image                    
  validates_attachment_size :image, :less_than => 10.megabytes
  validates_attachment_content_type :image, :content_type => ['image/jpeg', 'image/png', 'image/gif', 'image/jpg', 'image/JPG']

    after_post_process :update_users_space_used

    def self.total_used
        total_size = 0
        all.each do |upload|
            total_size += upload.image_file_size
        end
        return total_size
    end

    def update_users_space_used
        Authorization.current_user.space_used += self.image_file_size       
    end


end

Thanks.

Edit1: Btw, I am using paperclip to manage the uploads.

Edit2: In the Upload.rb model, I changed the before_save to the after_post_process callback for paperclip and it still doesn’t work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T19:14:44+00:00Added an answer on May 21, 2026 at 7:14 pm

    Seems like Paperclip’s after_post_process callback would be what you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm surprised this doesn't happen automatically, but I would like my applications window to
I'm doing a windows form and would like an audit task to happen every
Given the following: What I would like to happen: 1. Find all Span TAGS
I would like to have a simple login form (or any form for that
This is my first time setting up Hudson and I would like to do
I am looking for a plugin that will tag (subversion) every single build that
Suppose I have a function in a single threaded program that looks like this
I would like to concentrate on what happens when we say delete ptr. I
I would like Visual Studio to break when a handled exception happens (i.e. I
I would like to find a way to see what happens while my XAML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.