Thursday, 15 August 2013

How do I reference the same Carrierwave uploaded file in multiple Ruby model instances without reprocessing

How do I reference the same Carrierwave uploaded file in multiple Ruby
model instances without reprocessing

I have a Ruby model that is using Carrierwave to upload files to fog
storage. The problem is that when I create say 100 model objects they all
upload the same file. I need the model instances to reference the same
uploaded file. A one to many relationship where there are many model
instances and 1 file.
At the moment the file is an attribute called attachment on my model
messages.rb -
class Message < ActiveRecord::Base attr_accessible :body,
:remote_attachment_url, :from, :to, :status, :attachment, :campaign,
:version, :user_id, :SmsId, :response, :response_code, :client_id
mount_uploader :attachment, AttachmentUploader
I set attachment in my controller when I create a new Message instance
MessagesController.rb -
recipients.each do |value|
@message = Message.new(:attachment => params[:message][:attachment],
:campaign => message[:campaign], :version => message[:version], :to =>
value, :body => body, :status => status, :user_id => current_user.id,
:client_id => client.id )
I'm using Ruby 2.0, Rails 4

No comments:

Post a Comment