Inserting Data into Rails -
i'm new rails , had trouble figuring out how insert data db.
i'm reading csv , load values db. model looks this.
class book < activerecord::base belongs_to :subject end class subject < activerecord::base has_many :books end
...and data this:
science, book science, book b history, book c math, book d
i'm splitting csv rows delimiter.
how can insert data in way if subject exists, child book record added existing subject in event subject doesn't exist, new subject created too?
is best way go or recommend better approach.
no need reinvent wheel. add header row csv file, save subjects.csv , use built in fixtures library load data: http://api.rubyonrails.org/classes/fixtures.html
Comments
Post a Comment