Jerome
Jerome

Reputation: 6217

Handling a parameter in a model definition

The following request

Parameters: {"utf8"=>"✓", [...], "balance"=>"2", "file"=>#<ActionDispatch::Http::UploadedFile[...]

Is meant to process a file, whose processing is defined in a model

def self.import(file)
  Rails.logger.info @balance
  CSV.foreach(file.path, :col_sep => "\t", :quote_char => "\x00", headers: false) do |row|

However, this model needs to access the balance parameter as a global value.

The logger registers a blank for @balance. The controller attempts to pass this information to the model in the following manner

def import
  @balance = request.params[:balance]
  Importportfolio.import(params[:file])

How can the model effectively use this parameter?

Upvotes: 0

Views: 23

Answers (1)

Rockwell Rice
Rockwell Rice

Reputation: 3002

You have to send it with the method call.

def import
  @balance = request.params[:balance]
  Importportfolio.import(params[:file], @balance)
  ...

Then in the model

def self.import(file, balance)
  ...

Then in the model, you would reference it as balance not @balance, just to be clear

Upvotes: 1

Related Questions