0

I have a multitenant app and I use Mongoid.override_session(current_user.customer_id) to point to the appropriate customer database. I'm using paperclip to save photos to an S3 bucket and I'd like the folder structure to start with the customer_id but I can't find a way to get the current session or database from Mongoid. I found this question but the answers are not working for me on Mongoid v4.0.1. current_user is out of scope in the model code that sets up the URL for paperclip as is the paperclip initializer.

Community
  • 1
  • 1
SteveO7
  • 2,430
  • 3
  • 32
  • 40

1 Answers1

0

In case it helps someone else, here is a method I found that you can run on the class or an instance of your model

mongo_session.options[:database]

I added the following to my paperclip initializer;

#myapp/config/initializer/paperclip.rb  
Paperclip.interpolates :customer_id do |attachment, style|
  attachment.instance.mongo_session.options[:database]
end

Then you can reference :customer_id in your model

has_mongoid_attached_file :image,
                            :storage => :s3,
                            :s3_credentials => File.join(Rails.root, 'config', 'aws.yml'),
                            :styles => {
                                :medium => '600x600>',
                                :small => '300x300>',
                                :thumb => '100x100>'
                            },
                            :path => ':customer_id/:style/:filename',
                            :url => '/:customer_id/:style/:filename'
SteveO7
  • 2,430
  • 3
  • 32
  • 40