2011-04-30 31 views
0

我想要發生的是每上傳一個新文件,user.space_used已更新。如何在每次上傳新文件時獲取每個用戶帳戶的存儲空間 - 在Rails 3中?

但是,現在沒有發生。爲了獲得更新,我必須在控制檯上手動運行user.update_space以更新特定用戶,或者使用數組遍歷所有用戶並像那樣更新它。

我如何得到它在正確的時間做 - 而且,這將是很好,如果我能確認的User模式space_used列有所有文件的上傳該用戶的的filesizes的總和在Upload模型上。

我的用戶模型是這樣的:

# == 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 

我的上傳模式是這樣的:

# == 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 

感謝。

編輯1:順便說一句,我用回形針來管理上傳。

編輯2:在Upload.rb模型中,我將before_save更改爲after_post_process回形針回形針,它仍然不起作用。

回答

1

看起來像回形針的after_post_process回調將是你想要的。

+0

有什麼樣的例子嗎?我在github自述文件中看到它的引用,但沒有實現的例子。 – marcamillion 2011-04-30 22:21:46

+0

http://stackoverflow.com/questions/4528287/paperclip-callbacks-or-simple-processor有一個例子。 – carpeliam 2011-04-30 22:27:51

+0

不...沒有工作。我已經更新了問題中的代碼,以顯示我所做的事情。 – marcamillion 2011-04-30 22:36:51

相關問題