title: Rails has_many :through with custom foreign_key
body: <p>I have the following set of models:</p>
  
  <pre><code>class Cardstock &lt; ActiveRecord::Base
    has_many :color_matches, :primary_key =&gt; :hex, :foreign_key =&gt; :hex
    has_many :palette_colors, :through =&gt; :color_matches
  end
  
  class ColorMatch &lt; ActiveRecord::Base
    belongs_to :palette_color
    has_many :cardstocks, :foreign_key =&gt; :hex, :primary_key =&gt; :hex
  end
  
  class PaletteColor &lt; ActiveRecord::Base
    has_many :color_matches
    has_many :cardstocks, :through =&gt; :color_matches
  end
  </code></pre>
  
  <p>Calling <code>Cardstock.last.palette_colors</code> yields the following error:</p>
  
  <pre><code>ActiveRecord::StatementInvalid: PGError: ERROR:  operator does not exist: character varying = integer
  LINE 1: ...".palette_color_id    WHERE (("color_matches".hex = 66))  OR...
                                                               ^
  HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
  : SELECT "palette_colors".* FROM "palette_colors"  INNER JOIN "color_matches" ON "palette_colors".id = "color_matches".palette_color_id    WHERE (("color_matches".hex = 66))  ORDER BY name ASC
  </code></pre>
  
  <p>This shows me that the query ActiveRecord generates is using the cardstock's id (<code>66</code>) where it should be using the cardstock's hex (<code>bbbbaf</code>). Somewhere, I need to specify to ActiveRecord to use the <code>hex</code> column to join between <code>cardstocks</code> and <code>color_matches</code>. Does ActiveRecord support this?</p>