我有两列:
background_image_id (which is a relation)
background_image (which is a copy of that image, with various versions)
在我的before_update
方法中,我必须将 Carrierwave 文件从Medium.find(background_image_id)
列background_image
中复制。
而我做不到。NIL 被存储到数据库中。不知道为什么。
控制器:
def update
if params[:profile].blank? || params[:profile][:background_image_id].blank?
flash[:alert] = 'Please select image'
redirect_to edit_candidate_background_image_path
else
resource.background_image_id = params[:profile][:background_image_id]
if resource.save
flash[:notice] = 'Profile was updated'
else
flash[:alert] = "Can't update profile"
end
redirect_to edit_candidate_background_image_path
end
end
模型:
before_update :copy_background_image, if: :background_image_id_changed?
...
def copy_background_image
medium = Medium.find(background_image_id)
if medium.present?
self.background_image = medium.image.file
puts self.inspect # outputs a model where background_image is nil
puts self.background_image.inspect # outputs an uploader with correct image PRESENT
end
end