Sonal S.
Sonal S.

Reputation: 1442

Active record in rails

Is there any way to insert record from file to database using active record?

Suppose I have a file test.txt with the following content:

1,20,sonal
3,34,Ram

I have a table students having fields roll_nu, marks, and name.

I want to store the records of file in table using active record. How can I do this?

Upvotes: 0

Views: 36

Answers (1)

Hauleth
Hauleth

Reputation: 23556

It is simple CSV file, so you can:

CSV.parse('path/to/your/file.csv') do |row|
  Student.create(roll_nu: row[0], marks: row[1], name: row[2])
end

Upvotes: 3

Related Questions