0

This is semi-related to this question: How do I get the storage of each user account to update when a new file is uploaded - in Rails 3?

But it actually is a bit different, because it involves a specific action in my controller.

This is the controller action (upload_controller.rb):

def destroy
     upload = Upload.find(params[:id])
     upload.destroy
     respond_to do |format|
        format.html { redirect_to("/") }
        format.js   { render :json => ['upload',params[:id]].to_json, :layout => false }
    end
     current_user.update_space
end

This is my User.rb model:

# == 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
  has_friendly_id :username, :use_slug => true, :strip_non_ascii => true

    before_save :update_space

  has_many :uploads

  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_left
        (self.plan.storage * 1024 * 1024 * 1024) - self.space_used.to_f     
    end

end

The line in question is in the upload_controller.rb where it says current_user.update_space. That doesn't update the currently logged in user when an upload is destroyed.

How do I achieve that ?

Community
  • 1
  • 1
marcamillion
  • 32,933
  • 55
  • 189
  • 380

1 Answers1

3

You're attempting to run code after your controller has responded. You want to move this line:

 current_user.update_space

to above the respond_to call.

Ryan Bigg
  • 106,965
  • 23
  • 235
  • 261
  • That still doesn't work for some reason. Could it be that the method `update_space` is declared in the `User` model and this was being called in the `upload_controller` ? – marcamillion May 01 '11 at 02:59
  • 1
    @marcamillion: No, that has nothing to do with it. Controllers and models are linked ONLY by name. A controller can access any model. Your `update_space` method isn't saving the user record after it updates the space. You need to call `save` at the end of the model. – Ryan Bigg May 01 '11 at 04:38
  • 1
    I tried that, and I got `!! Unexpected error while processing request: deadlock; recursive locking`. I think that is because in my User model, I am calling `update_space` as a `before_save`. So by adding `self.save` to my `update_space` method, it throws everything into crazy town. So I removed the `before_save` from my `User` model and re-jigged things a bit. Now the space is updated after every upload and every delete - which is exactly what I want. – marcamillion May 01 '11 at 07:49