title: Rails: Linking two models that inherit the same model
body: <p>This is a little bit tricky, so if you need more information, don't hesitate!</p>
  
  <p>I have two models, <code>Store</code> and <code>Consumer</code> that are linked by two ways:</p>
  
  <p>1/ <code>Store</code> and <code>Consumer</code> inherite from the same model <code>Profile</code>, because they share many attributes (name, location, email, web page,...). Here is the Rails AR code:</p>
  
  <pre><code>class Profile &lt;&lt; ActiveRecord::Base
      # Attributes and validation rules go here.
  end
  
  class Store &lt;&lt; Profile
  end
  
  class Consumer &lt;&lt; Profile
  end
  </code></pre>
  
  <p>This is the well known Single Table Inheritance (STI).</p>
  
  <p>2/ In addition to STI, <code>Store</code> and <code>Consumer</code> are linked by a many to many relation:</p>
  
  <ul>
  <li><p>Store has many Clients (many consumers)</p></li>
  <li><p>A consumer is client to many stores</p></li>
  </ul>
  
  <p>Because I need more attributes for this link (Store - Consumer), I have to create an extra model that will link them: <code>Client</code>.</p>
  
  <p>Here are my final AR models:</p>
  
  <pre><code>class Profile &lt;&lt; ActiveRecord::Base
      # Attributes and validation rules go here.
  end
  
  class Store &lt;&lt; Profile
      has_many :clients
  end
  
  class Consumer &lt;&lt; Profile
      has_many :clients
  end
  
  class Client &lt;&lt; ActiveRecord::Base
      belongs_to :store
      belongs_to :consumer
  end
  </code></pre>
  
  <p><strong>Problem</strong></p>
  
  <p>Using STI doesn't create store_id and consumer_id... we have only profile_id (because one real table <code>Profile</code>). So, how can I target the correct <code>Client</code> row having both store_id and client_id ?</p>
  
  <p>Any idea how to do that? Thanks in advance.</p>