title: How to work with rich join tables in Rails
body: <p>I am stuck struggling with rich join tables in Rails 5 and need help getting on track.  The app I'm writing will help me track which of our company's suppliers carry which brands of products.  Because I also need to track whether each supplier is authorized or unauthorized for each of the brands they sell, and whether they carry those brands in stock, I thought the best approach was to use a join table and store the attributes there.  In other words:</p>
  
  <pre><code>Suppliers &lt;---&gt; Lines &lt;---&gt; Brands
  </code></pre>
  
  <p>Beyond the foreign key references for a <code>Supplier</code> and a <code>Brand</code>, the <code>Line</code> record also has two boolean attributes: <code>.is_authorized</code> and <code>.carries_stock</code>.</p>
  
  <p>My models:</p>
  
  <p><strong>/models/supplier.rb</strong></p>
  
  <pre><code>class Supplier &lt; ApplicationRecord
    has_many :lines, :dependent =&gt; :destroy
    has_many :brands, :through =&gt; :lines
  
    accepts_nested_attributes_for :lines
  end
  </code></pre>
  
  <p><strong>/models/brand.rb</strong></p>
  
  <pre><code>class Brand &lt; ApplicationRecord
    has_many :lines, :dependent =&gt; :destroy
    has_many :suppliers, :through =&gt; :lines
  end
  </code></pre>
  
  <p><strong>/models/line.rb</strong></p>
  
  <pre><code>class Line &lt; ApplicationRecord
    belongs_to :supplier
    belongs_to :brand
  
    validates_presence_of :supplier
    validates_presence_of :brand
  end
  </code></pre>
  
  <p>I've been able to set up the controller and supplier edit form to allow creating records in the <code>Lines</code> table, but have no clue how to allow the users to edit the <code>.is_authorized</code> and <code>.carries_stock</code> attributes.  I have been able to get the create/edit supplier form to work by adding the following snippet:</p>
  
  <p><strong>/views/suppliers/_form.html.erb</strong></p>
  
  <pre><code>  &lt;h4&gt;Brands&lt;/h4&gt;
    &lt;%= form.collection_check_boxes(:brand_ids, Brand.all, :id, :name) do |b| %&gt;
      &lt;%= b.label class:"label-checkbox" do%&gt;
        &lt;%= b.check_box + b.text%&gt;
      &lt;%end%&gt;
      &lt;br /&gt;
    &lt;% end %&gt;
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/8t9Gq.png" rel="nofollow noreferrer">The form looks like this now</a> but doesn't allow me to edit the rich attributes <code>.is_authorized</code> and <code>.carries_stock</code>.  I'd like the form <a href="https://i.stack.imgur.com/Sexxh.png" rel="nofollow noreferrer">to look something more like this</a>.  Where do I go from here?</p>
  
  <p>Thanks!!!</p>