title: How to update related attributes while saving objects in Rails Active Record
body: <p>My current logic is, taking two inputs from form where one is primary book and one is secondary book and save it to database with is_primary check being true for primary book and false for secondary. We can have multiple secondary books.</p>
  <p>I have a table books:
  Table name: books</p>
  <ul>
  <li>book_id</li>
  <li>book_name</li>
  </ul>
  <p>And I have another table authors:
  Table name: authors</p>
  <ul>
  <li>author_id</li>
  <li>author_name</li>
  </ul>
  <p>And I have relation table as below:
  Table name: author_book</p>
  <ul>
  <li>author_book_id</li>
  <li>book_id</li>
  <li>author_id</li>
  <li>is_primary (Bool)</li>
  </ul>
  <p>Code for form:</p>
  <pre><code>  &lt;div class=&quot;elem-set&quot;&gt;
      &lt;div class=&quot;elem-label&quot;&gt;
        &lt;label&gt;Author&lt;/label&gt;
      &lt;/div&gt;
  
      &lt;%= f.select :associated_author,
      Author.sort_by_name.map{|t| [&quot;#{t.id}: #{t.name}&quot;, t.name]},
      { include_blank: &quot;No Author&quot; },
      { class: 'chzn-select', multiple: false }
      %&gt;&lt;br /&gt;&lt;br /&gt;
    &lt;/div&gt;
  
  
    &lt;div class=&quot;elem-set&quot;&gt;
      &lt;div class=&quot;elem-label&quot;&gt;
        &lt;label&gt;Primary Book&lt;/label&gt;
      &lt;/div&gt;
  
      &lt;%= f.select :associated_primary_book,
      Book.sort_by_name.map{|n| [&quot;#{n.book_name}: #{n.book_id}&quot;, n.book_name]},
      { include_blank: &quot;No Book&quot; },
      { class: 'chzn-select', multiple: false }
      %&gt;&lt;br /&gt;&lt;br /&gt;
    &lt;/div&gt;
  
  
    &lt;div class=&quot;elem-set&quot;&gt;
      &lt;div class=&quot;elem-label&quot;&gt;
        &lt;label&gt;Secondary Book&lt;/label&gt;
      &lt;/div&gt;
  
      &lt;%= f.select :associated_secondary_book,
      Book.sort_by_name.map{|n| [&quot;#{n.book_name}: #{n.book_id}&quot;, n.book_name]},
      { include_blank: &quot;No Book&quot; },
      { class: 'chzn-select', multiple: true }
      %&gt;&lt;br /&gt;&lt;br /&gt;
    &lt;/div&gt;
  </code></pre>
  <p><strong>MODEL</strong>
  Book class:</p>
  <pre><code>class Book &lt; ActiveRecord::Base
      has_many :author_book, dependent: :destroy
      has_many :authors, through: :author_book
  
      scope :sort_by_name, -&gt; { select([:book_id, :book_name]).order('book_name ASC') }
  end
  </code></pre>
  <p>Authors class:</p>
  <pre><code>class Author &lt; ActiveRecord::Base
    has_many :author_book, dependent: :destroy
    has_many :books, through: :author_book
    def associated_primary_books=(fetched_books)
      self.books.clear
      if !fetched_books.empty?
          self.books &lt;&lt; Book.find_by_name(fetched_books)
          #is_primary=&gt; true
      end
    end
  
  
    def associated_secondary_books=(fetched_books)
      fetched_books = fetched_books.delete_if { |x| x.empty? }
      if !fetched_books.empty?
        fetched_books.each do |book|
          self.books &lt;&lt; Book.find_by_name(book)
        end
      end
    end
    
  end
  </code></pre>
  <p>AuthorBook class:</p>
  <pre><code>class AuthorBook &lt; ActiveRecord::Base
    belongs_to :authors
    belongs_to :books
    
  end
  </code></pre>
  <p><strong>Controller</strong>
  Author controller</p>
  <pre><code>AuthorController &lt; ApplicationController
    def create
      @Author = Author.new(params[:author])
  
      respond_to do |format|
        begin
          retries ||= 0
          if @author.save
            flash[:notice] = 'Author created.'
            Event.init(user: current_user.id, type: &quot;Create Author&quot;, details: {
              :author =&gt; @author.id
            })
            format.html { redirect_to action: 'show',
                  :id =&gt; @author.id
            }
          end
        rescue ActiveRecord::StatementInvalid =&gt; e
          flash[:error] = 'error'
          redirect_to action: 'new'
        end
      end
    end
  </code></pre>
  <p><strong>ISSUE</strong>: What I want to achieve is whenever I save book object, I also somehow save the <code>is_primary</code> as true or false which is present in author_book table.</p>
  <ol>
  <li>When I try to add book to author, it gets saved successfully. But I am unable to update the is_primary attribute resulting in all
  books as secondary. I get undefined method error if I try to update
  the is_primary attribute. How do I access the is_primary attribute
  which is present in relation table and solve this cleanly?</li>
  <li>If I try to update the author_book table after adding Book, it returns empty array because everything is saved after Author is
  created.</li>
  <li>This is stub code with example I created and not the complete code.</li>
  </ol>
  <p>We are using Rails 3.</p>