title: has_many and belongs_to with join table
body: <p>I have <code>User</code>s and <code>Truck</code>s. I want the ability to say <code>@truck.drivers &lt;&lt; @users</code> and <code>@user.truck = @truck</code>.</p>
  
  <p>The solution is simple until I want the relationship to be stored in a join table.</p>
  
  
  
  <pre><code># tables
  :users
    :id
  
  :truck_drivers
    :user_id
    :truck_id
  
  :truck
    :id
  </code></pre>
  
  <p>I've gotten it to where I can say <code>@truck.drivers &lt;&lt; @user</code> and <code>@user.trucks &lt;&lt; @truck</code>, but I would like to limit a user to occupy one truck at a time, for my sanity.</p>
  
  <p>Is it possible? A has_many/belongs_to with a join table? Or should I try a different approach? I'm not using a third model for the join table. It's just a table. Here's what I have so far.</p>
  
  <pre><code>class User &lt; ApplicationRecord
    has_and_belongs_to_many :trucks,
      join_table: :truck_drivers, # points to the table
      class_name: :Truck # looks for the truck model in the table
  end
  
  class Truck &lt; ApplicationRecord
    belongs_to :company
    has_and_belongs_to_many :drivers,
      join_table: :truck_drivers,
      class_name: :User
  end
  </code></pre>
  
  <p>The reason I need a join table in the first place is because each <code>User</code> can have many <code>Role</code>s: Admin, Manager, Driver, Customer Service, etc. I thought it didn't make sense to add a <code>truck_id</code> to all the users if all the users are not going to be using trucks. </p>