title: Rails: Two different many to many relationships between two models
body: <p>I have two tables named <code>books (id, name, author_name)</code> and <code>users (id, name, location)</code>. A book can either be viewed by a user or edited by a user. So, for these two relationships I have two join tables viz. <code>book_editor_users (book_id, user_id)</code> and <code>book_viewer_users (book_id, user_id)</code>.
  How do I model this in Rails such that I can retrieve editor users and viewer users like this:</p>
  
  <pre><code>Book.find(1).book_editor_users
  Book.find(1).book_viewer_users
  </code></pre>
  
  <p>My attempt for the book and user model are:</p>
  
  <pre><code>class Book &lt; ActiveRecord::Bas
    has_many :book_editor_users
    has_many :users, through: :book_editor_users
    has_many :book_viewer_users
    has_many :users, through: :book_viewer_users # I am confused on how to setup this line
  end
  
  class User &lt; ActiveRecord::Base
    has_many :books, through: :book_editor_users
    has_many :books, through: :book_viewer_users # I am confused here too
  end
  </code></pre>
  
  <p>Join models I have written are:</p>
  
  <pre><code>class BookEditorUser &lt; ActiveRecord::Base
    belongs_to :book
    belongs_to :user
  end
  
  class BookViewerUser &lt; ActiveRecord::Base
    belongs_to :book
    belongs_to :user
  end
  </code></pre>
  
  <p>There is another work around that I thought of, but I am not sure whether it is the Rails way. That work around is to have a single join table <code>book_users (book_id, user_id, type)</code> where the type column can capture whether it is an editor relationship or a viewer relationship.</p>