BDubbs
BDubbs

Reputation: 103

Importing a csv to Active Record but adding an attribute

I have a question on implementing this in Rails 3. I have been looking at the default CSV class as well as gems like csv-mapper.

When importing a csv the code looks like this

def self.import(file)
  CSV.foreach(file.path, headers: true) do |row|
    Product.create! row.to_hash
  end
end

But I would like to add an attribute to each record that is imported for example '000002' into the :account column. How exactly would I modify the above to do that. Sorry if this is an obvious answer I am still a beginner/intermediate (The most dangerous level ;-) in rails and ruby

Upvotes: 0

Views: 135

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230551

If the value is static, then it's easy as pie:

Product.create! row.to_hash.merge(account: '000002')

Upvotes: 3

Related Questions