Ruby on rails 3 carrierwave中的路径结构,其名称未包含在模型中
我的模型如下:Ruby on rails 3 carrierwave中的路径结构,其名称未包含在模型中,ruby-on-rails-3,carrierwave,Ruby On Rails 3,Carrierwave,我的模型如下: class User < ActiveRecord::Base has_one :info, :class_name => "PostulantInfo" mount_uploader :curriculum_vitae, DocumentUploader #... class PostulantInfo < ActiveRecord::Base belongs_to :user has_many :relatives has_many
class User < ActiveRecord::Base
has_one :info, :class_name => "PostulantInfo"
mount_uploader :curriculum_vitae, DocumentUploader
#...
class PostulantInfo < ActiveRecord::Base
belongs_to :user
has_many :relatives
has_many :studies, :class_name => "HighSchoolStudy", :foreign_key => :postulant_info_id
mount_uploader :marriage_certificate, DocumentUploader
mount_uploader :photo1, FaceImageUploader
mount_uploader :photo2, SideFaceImageUploader
#...
class Relative < ActiveRecord::Base
mount_uploader :birth_certificate, DocumentUploader
#...
class HighSchoolStudy < ActiveRecord::Base
mount_uploader :degree_certificate, DocumentUploader
#...
这就是创建一个目录结构,如:
- uploads
- user
- curriculum_vitae
- {user.id}
- {the_file}
-postulant_info
- marriage_certificate
- {postulant_info.id}
- {the_file}
- photo1
- {postulant_info.id}
- {the_file}
- photo2
- {postulant_info.id}
- {the_file}
- and so on...
但我想要的是:
- uploads
- {user.uuid}
- {all_the_files_for_this_user}
def store_dir
"uploads/#{model.user.uuid}"
end
问题是我不知道如何给出user.uuid
,例如,当我想保存posturantinfo
的marry\u证书
文件时。我查找了一段时间,但找不到有助于解决此问题的方法。考虑一种可能的解决方案,可以为包含文件的每个模型添加一个uuid属性,然后将user.uuid复制到每个模型并保存到数据库中,但这将浪费资源。
一个不那么麻烦的解决方案可以是访问父模型,例如访问
posturantinfo
model,并执行以下操作:
- uploads
- {user.uuid}
- {all_the_files_for_this_user}
def store_dir
"uploads/#{model.user.uuid}"
end
但这意味着:
- 为每个级别制作一个上传器
- 添加
到属于\u to:positionLantInfo
相对
和
高中学习
,这些模型没有理由知道他们的父母(除了知道uuid)
- 每次我想要呈现一个指向该文件的链接时,rails都会进行一次db查询,以了解uuid,这是我在
current\u user.uuid中设计的