Carrierwave Rails 3 S3,将文件大小保存到数据库 [英] Carrierwave Rails 3 S3, save the file size to the database
问题描述
在Rails 3.2.6中使用Carrierwave.一切正常,除了我需要对表格进行排序以外,其中一些附件按文件大小显示.我正在使用S3进行防雾存储.
Using Carrierwave with Rails 3.2.6. All fine, except I need to sort a table where some attachments are displayed by file size. I'm using S3 for storage with fog.
假设我有一个载波显示如下:
Let's say I have a Carrierwave showing like this:
<%= @project.attachment %>
我可以通过在字段名称后使用'.size'来显示文件的大小:
I am able to show the size of the file by using '.size' after the field name:
<%= @project.attachment.size %>
以字节为单位显示文件大小,但是由于从数据库获取记录时需要使用order子句,因此无法对此进行排序. 上传文件后,有什么方法可以将文件大小写到数据库中的特定列上,以便我进行排序吗?
shows the file size in bytes, but as I need to use an order clause when getting the records from the database, I cannot sort on this. Is there any way to write the file size to a particular column in the database after it has been uploaded so I can sort on this??
非常感谢
推荐答案
这对我有用
before_save :update_project_attributes
private
def update_project_attributes
if project.present? && project_changed?
self.file_size = project.file.size
end
end
这篇关于Carrierwave Rails 3 S3,将文件大小保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!