Carrierwave Rails 3 S3, save the file size to the database

梦想与她 提交于 2019-12-06 06:16:52

this worked for me

 before_save :update_project_attributes

 private

 def update_project_attributes
    if project.present? && project_changed?
      self.file_size = project.file.size
    end
 end

You should add a virtual attribute to the model and define a custom getter method that returns the file size. You can then sort with respect to this virtual attribute as you usually would. Let me know if you need more details and I will try to provide them!

Ok, got this to work with before_save

before_save :set_size

def set_size
  self.size = self.upload.size
end

where upload is the mounted field and size is a new db column to store the size.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!