title: Update additional association attribute before saving a model
body: <p>I have three models <code>Article</code>, <code>Author</code>, and <code>AuthorLine</code> presenting relationship between article and its authors, in a many-to-many mapping.</p>
  
  <pre><code>class Article &lt; ActiveRecord::Base                                                                                                                                                                
    has_many :author_lines, :dependent =&gt; :destroy                                                                                                                                                                          
    has_many :authors, :through =&gt; :author_lines, :dependent =&gt; :destroy, :order =&gt; 'author_lines.position'   
  
    attr_accessor :author_list
  end
  
  class Author &lt; ActiveRecord::Base                                                                                                                                                                 
    has_many :author_lines                                                                                                                                                                          
    has_many :articles, :through =&gt; :author_lines                                                                                                                                                   
  end
  
  class AuthorLine &lt; ActiveRecord::Base                                                                                                                                                             
    validates :author_id, :article_id, :position, :presence =&gt; true
  
    belongs_to :author, :counter_cache =&gt; :articles_count                                                                                                                                           
    belongs_to :article                                                                                                                                                                             
  end
  </code></pre>
  
  <p>The <code>AuthorLine</code> model has an additional attribute <code>position</code>, which tells the order of authors for an article.</p>
  
  <p>Here is what I am doing to create an article with given author names, in article.rb:</p>
  
  <pre><code>def author_list=(raw)                                                                                                                                                                           
    self.authors.clear                                                                                                                                                                            
    raw.split(',').map(&amp;:strip).each_with_index do |e, i|                                                                                                                                         
      next if e.blank? 
      author = Author.find_or_create_by_name(e)                                                                                                                                                   
  
      #1                                                                                                                                                                      
      self.authors &lt;&lt; author                                                                                                             
  
      #2
      # AuthorLine.create(:author_id =&gt; author.id, :article_id =&gt; self.id, :position =&gt; i)                                                                                                        
    end                                                                                                                                                                                           
  end
  </code></pre>
  
  <p>The problem is I have no idea when to update the <code>position</code> attributes of corresponding <code>AuthorLine</code>s. if I remove the line #1 and uncomment the line #2, the created AuthorLine may have a nil <code>arctile_id</code> since <code>self.id</code> may not be given.</p>