Railroad Tycoon
Railroad Tycoon

Reputation: 147

Accessing current_user in one class

I am using the gem csv-importer, which gives me a class that I use to import csv data into my rails database

However, I need to know the current user when I am uploading a csv file, but Rails does not grant access to current_user in the class

I would like to pass custom parameters (such as current_user data) from my controller into the import_csv class when it is being called, how do I do that?

the import_csv class is called using

import = ImportUserCSV.new(file: file)
import.run!

I tried a solution in which I have current_user available for all classes, this worked for localhost but broke on Heroku

Upvotes: 0

Views: 84

Answers (1)

PGill
PGill

Reputation: 3521

Try

import = ImportUserCSV.new(file: my_file) do
  after_build do |user|
     # here you have access to current_user 
  end
end

Upvotes: 1

Related Questions