title: Inserting Data into Rails
body: <p>I'm new to rails and had trouble figuring out how to insert data into the db.</p>
  
  <p>I'm reading a CSV and would like to load those values into the db. My model looks something like this.</p>
  
  <pre><code>class Book &lt; ActiveRecord::Base
    belongs_to :subject
  end
  
  class Subject &lt; ActiveRecord::Base
    has_many :books
  end
  </code></pre>
  
  <p>...and my data is something like this:</p>
  
  <pre><code>Science, Book A
  Science, Book B
  History, Book C
  Math, Book D
  </code></pre>
  
  <p>I'm splitting the CSV rows by the delimiter. </p>
  
  <p>How can insert the data in a way that if a subject exists, the child book record will be added to the existing subject but in the event that a subject doesn't exist, a new subject will be created too?</p>
  
  <p>Is this the best way to go about this or could someone recommend a better approach.</p>