1

I followed this tutorial to import a csv file. The tutorial explains how to exclude certain fields:

Player.create!(player_hash.except("number"))

Is it possible to add certain fields when creating a new player from the cvs? For example: create a new player by getting the name from the cvs file and add the team_id that is not in the csv file. Something like this:

Player.create!(player_hash.add("team_id"))

But this throws an error:

undefined method 'add'

Is there a way to add a field?

1 Answer 1

2

The method you are looking for is .merge(another_hash). So you should do something like:

Player.create!(player_hash.except("number").merge({ team_id: some_id }))
Sign up to request clarification or add additional context in comments.

Comments

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.