title: How to add existing records to has_many without it saving to the DB immediately?
body: <p>Suppose I've got this:</p>
  
  <pre><code>class Pirate &lt; ActiveRecord::Base
    has_many :parrots
    validates_presence_of :name
  end
  
  class Parrot &lt; ActiveRecord::Base
    belongs_to :pirate
  end
  </code></pre>
  
  <p>And I've got existing Pirates and Parrots with ids 1 to 10. Now I'd like to do this:</p>
  
  <pre><code>p = Pirate.first
  p.name = nil
  p.parrot_ids = [1,2,3]
  p.save if p.valid?
  </code></pre>
  
  <p>Because the pirate object is not valid (it's missing a name) I don't want it to be saved. However, the parrots are linked to the pirate now, and it's committed in the database.</p>
  
  <p>How can I assign the parrots, but have the links to the parrots only saved to the database when p.save is successful? I.e., how can I save the pirate and the links to the parrots in one transaction?</p>