2

The following import method needs to handle input variables set as nil

def self.import(file, unit_id, profit_center_column [...]
  CSV.foreach(file.path, :col_sep => "\t", headers: true, skip_blanks: true) do |row|
    begin
     Asset.create(
        unit_id: unit_id,
        profit_center_id: row[profit_center_column] unless profit_center_column.nil?,
        [...]

Where the controller launching this method defines

params[:profit_center_column] = @unit.profit_center_column

where @unit.profit_center_column = nil.

I am hitting an unexpected syntax error, unexpected modifier_unless, expecting ')' I have never had to use parenthesis before using unless and I woudl expect the use of the comma here to clearly seperate the statements. Where is the syntax getting messed up?

Rails 4.2.4 and Ruby 2.3.4 are in use.

Note, I've also attempted if statements, but that led to loading random xxx_column information.

1 Answer 1

3

You're passing a conditional to the Asset's constructor. If you need to take care of it I'd suggest first initializing Asset with ensured not nil values and afterwards using setters for the remaining attributes:

asset = Asset.new(
    unit_id: unit_id,
    ....

asset.profit_center_id = row[profit_center_column] unless profit_center_column.nil?
asset.save
Sign up to request clarification or add additional context in comments.

2 Comments

Yes... create is new plus save ... was focussing on the tree, not the forest!
actually the conditional should be asset.profit_center_id = [...]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.