title: ActiveRecord association getting saved before the record in an update
body: <p>I have an <code>Entry</code> model which has many <code>Tag</code>s. <code>Tag</code>s are added to an entry by typing them into a textbox on my form, via a <code>tag_names</code> virtual attribute. Before validation on the <code>Entry</code> model, the tag_names string is converted into actual <code>Tag</code> objects using <code>find_or_create_by_name</code>. The <code>Tag</code> model also has validation to make sure the tag name matches a regex, which is run via the association.</p>
  
  <p>My Entry model looks like this:</p>
  
  <pre><code>class Entry &lt; ActiveRecord::Base
    has_many :entry_tags
    has_many :tags, :through =&gt; :entry_tags
  
    before_validation :update_tags
  
    attr_writer :tag_names
  
  private
    def update_tags
      if @tag_names
        self.tags = @tag_names.split(",").uniq.map do |name|
          Tag.find_or_create_by_name(name.strip)
        end
      end
    end
  end
  </code></pre>
  
  <p>When I create a new <code>Entry</code> object and assign it tags, everything works correctly -- the tags are not saved if there is a validation error on one of the <code>Tag</code>s, and an error message is passed back. However, if I try to update an existing Entry object with an invalid tag, instead of passing back a message, my <code>self.tags=</code> call (in <code>update_tags</code> above) is throwing an exception with the validation error message. Even if I overwrite <code>find_or_create_by_name</code> to actually just return a new object instead of calling <code>create</code>, I get the same outcome.</p>
  
  <p>It seems to me (and the <a href="http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html" rel="nofollow noreferrer">docs</a> seem to corroborate) that the <code>tags=</code> call is actually saving my <code>Tag</code> objects before the main record gets saved when the <code>Entry</code> object already exists. Is there anything I can do to make this save not happen, or to stop it from raising an exception and just causing my save to return false?</p>