I am new to rails and don't know how to insert data into db.
I am reading CSV and want to load these values in db. My model looks something like this.
class Book < ActiveRecord::Base
belongs_to :subject
end
class Subject < ActiveRecord::Base
has_many :books
end
... and my data is something like this:
Science, Book A
Science, Book B
History, Book C
Math, Book D
I am separating CSV lines with a delimiter.
How to insert data so that if an object exists, a child’s book entry will be added to the existing topic, but if the object does not exist, a new object will be created?
This is the best way to do this, or someone may recommend a better approach.
source
share