Ticket for Review #765: Add :primary_key option to belongs_to association

Hi -

Several weeks ago ticket #292 added the ability to specify the primary
key in a has_many relationship and use that field instead of the
primary key.


This doesn't exist for the belongs_to side of that relationship.
Ticket 765 (hopefully) fixes this.


With this patch applied one can now do this:

..... schema ......

    create_table :states do |t|
      t.string :abbr
      t.string :name

    create_table :cities do |t|
      t.string :state_abbr
      t.string :name

..... models ......

class City < ActiveRecord::Base
  belongs_to :state, :primary_key => :abbr, :foreign_key => :state_abbr

class State < ActiveRecord::Base
  has_many :cities, :primary_key => :abbr, :foreign_key => :state_abbr

And assuming the tables are populated with the data you'd expect, you
can use the association as normal.

>> wa = State.find(1)
=> #<State id: 1, abbr: "WA", name: "Washington">
=> #<City id: 1, state_abbr: "WA", name: "Olympia">
>> wa.cities.first.state
=> #<State id: 1, abbr: "WA", name: "Washington">

Thanks for taking a look!