Q: Odd ORM behavior for rails query with non id for primary key Given a model backed by a legacy table, where the primary key is unique_id and not id when I query the model with an id (e.g. CustomerLocations.where(id: 123)) the query finds the correct record but displays the wrong value for the id column.  Moreover, if I reference the id of that record, I get the primary key (i.e. unique_id) and not the id.

Note the id column is 4457777 and the PK is 53710
irb(main):001:0> customer = CustomerLocations.where(id: 445777).first
  CustomerLocations Load (0.3ms)  SELECT `customer_locations`.* FROM `customer_locations` WHERE `customer_locations`.`id` = 445777
=> #<CustomerLocations id: 53710, unique_id: 53710, name: "Joe Smith", ... >

irb(main):002:0> customer.first.id
=> 53710 # this is the unique id, should have returned 445777

If I run the same SQL query, the id value returned is the correct 445777, the non-pk.
mysql> SELECT id, unique_id FROM customer_locations WHERE id = 445777;
+--------+-----------+
| id     | unique_id |
+--------+-----------+
| 445777 |     53710 |
+--------+-----------+
1 row in set (0.00 sec)


Here's the model:

class LegacyBase < ActiveRecord::Base
  self.abstract_class = true
  establish_connection "ourlegacyprefix_#{Rails.env}".to_sym
  #...
end

class CustomerLocations < LegacyBase
  self.primary_key = :unique_id
  self.table_name = "customer_locations"

  belongs_to :customer, foreign_key: 'id'
  #...
end

class LegacyCustomer < GnpcbBase
  self.table_name = "legacy_customers"
  has_many  :customer_locations,
    foreign_key: 'id',
    dependent: :destroy

  belongs_to :customer_location
  #...
end

class Customer < ActiveRecord::Base
  belongs_to :legacy_customer
  #...
end

Here's some of my gemspec
ruby '2.3.1'

gem 'rails', '5.0.0'
gem 'mysql2', '0.4.4'

Here's the schema for customers and customer_locations
  create_table "customer_locations", primary_key: "unique_id", force: :cascade do |t|
    t.integer "id",            limit: 4,     default: 0,    null: false
    t.string  "location_id",   limit: 10,    default: "",   null: false
    t.string  "name",          limit: 60
    t.text    "address",       limit: 65535
    t.string  "city",          limit: 50
    t.string  "state",         limit: 50
    t.string  "zip",           limit: 20
    t.string  "country",       limit: 2,     default: "US", null: false
    t.string  "phone",         limit: 50
    t.string  "email",         limit: 100
    t.string  "location_type", limit: 20
    t.date    "last_modified"
    t.string  "checksum",      limit: 8
  end

  add_index "customer_locations", ["id"], name: "id", using: :btree
  add_index "customer_locations", ["location_id"], name: "location_id_unique", unique: true, using: :btree

  create_table "customers", force: :cascade do |t|
    t.string "name",              limit: 255, default: "", null: false
    t.string "last_name",         limit: 50,  default: "", null: false
    t.string "first_name",        limit: 30,  default: "", null: false
    t.string "customer_category", limit: 20,  default: "", null: false
    t.string "customer_type",     limit: 20,  default: "", null: false
    t.string "customer_class",    limit: 20,  default: "", null: false
    t.date   "last_modified"
  end

Is this a misconfiguration of my models or a Rails bug?
EDIT
Note:  If I set the primary key to :id on customer_locations then it works, but that of course conflicts with the pk of the table structure.

A: Because remember id is a method, not actually column. If you call the method id, it will return the primary key value. I think you need to fix your set up first if possible. Or, for now you can override the id method inside the model like:
def id
  _read_attribute(:id)
end


A: Pretty messy situation...
I would try to remap the id column:
class CustomerLocations < LegacyBase
  ...
  alias_attribute :secondary_id, :id
end

so you can access it through secondary_id
