How do I reference the same Carrierwave uploaded file in multiple Ruby model instances without reprocessing -
i have model using carrierwave uploader upload files fog storage.
the problem when create 100 model objects upload same file.
i need model instances reference same uploaded file. one-to-many relationship there many model instances , 1 file.
at moment file attribute called attachment on 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 end
i set attachment in controller when create new message instance in messagescontroller.rb -
recipients.each |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 ) end
i'm using ruby 2.0, rails 4
solution:
i fixed pushing attachment file new model building association between message object , attachment object.
messagescontroller:
@attachment = attachment.create(params[:message][:attachment_attributes]) recipients.each |value| @message = message.new(:campaign => params[:message][:campaign], :version => params[:message][:version], :to => value, :body => params[:message][:body], :status => status, :user_id => current_user.id, :client_id => client.id ) @message.attachment = @attachment end
message model:
attr_accessible :attachment_id, :attachment_attributes belongs_to :attachment accepts_nested_attributes_for :attachment
attachment model:
attr_accessible :attached, :remote_attached_url, :attachment_attributes mount_uploader :attached, attachmentuploader
if in model mounts carrierwave uploader have property:
mount_uploader :attachment, youruploader
so assuming new object created, should able assign image this:
newobj.attachment = anotherobj.attachment newobj.save
let me know if works.
Comments
Post a Comment