Ruby on rails carrierwave和mongoid的回调问题
我在rails 3应用程序上使用carrierwave和mongoid,在保存后回调时遇到问题。考虑下面的Ruby on rails carrierwave和mongoid的回调问题,ruby-on-rails,mongoid,carrierwave,Ruby On Rails,Mongoid,Carrierwave,我在rails 3应用程序上使用carrierwave和mongoid,在保存后回调时遇到问题。考虑下面的 class Video include Mongoid::Document field :name mount_uploader :file, VideoUploader after_create :enqueue_for_encoding protected def enqueue_for_encoding // point your en
class Video
include Mongoid::Document
field :name
mount_uploader :file, VideoUploader
after_create :enqueue_for_encoding
protected
def enqueue_for_encoding
// point your encoding service to where it expects the permanent file to reside
// in my case on s3
end
end
我的问题是,在我的enqueue\u for_encoding
方法中,file.url指向本地tmp目录,而不是s3目录
当file.url指向s3时,如何让我的为编码方法排队
谢谢
Jonathan在模型中创建
回调后,您可以尝试删除,并将以下内容添加到上载程序中:
# video_uploader.rb
process :encode
def encode
model.enqueue_for_encoding
end
进程
回调是在文件保存后调用的(我认为),这应该允许您在文件在S3上启动后进行连接。好的,我已经解决了。我花了一点时间。因此当前carrierwave不公开after_create钩子,所有的持久化和处理都发生在after_save回调中。下面是我用来解决这个问题的代码:
# Video.rb
mount_uploader :file, VideoUploader
# overwrite the file setting to flag the model that we are creating rather than saving
def file=(obj)
@new_file = true
super(obj)
end
# chain the store_file! method to enqueue_for_encoding after storing the file AND
# if the file is new
alias_method :orig_store_file!, :store_file!
def store_file!
orig_store_file!
if @new_file #means dirty
@new_file = false
enqueue_for_encoding
end
true
end
更新
呜呜,那没用。它几乎做到了——url是正确的,但它将被永久激发。这意味着文件仍在加载过程中,并且在调用enqueue_for_encoding时未完全存储该文件
这对我很有效可以在上传器上设置排队编码回调。但我更喜欢这样做:
class Video
# mount the uploader first:
mount_uploader :file, VideoUploader
# then add the callback:
after_save :enqueue_for_encoding, on: :create
end
感谢man的评论——但它没有起作用——甚至进程回调也指向temp file.dam。真烦人。您能否在应用程序中使用cron作业来扫描S3存储桶/目录中未处理的作业,并以这种方式将它们添加到队列中?虽然没有那么优雅,但应该能可靠地完成任务。