Q:

Model association on foreign key that is not integer

I'm building an API in rails, and I have 2 models :
Vehicle which has_one: :document
class CreateVehicles < ActiveRecord::Migration
   def change
      create_table :vehicles do |t|
          t.string :uuid, limit: 36, null: false, index: true
          t.string :license_plate_id
          t.integer :mileage
          t.string :mileage_unit, default: 'km'
          t.belongs_to :user, index: true

          t.timestamps null: false 
       end
    end
end

Vehicle.rb
class Vehicle < ActiveRecord::Base
  audited

  include UuidConcern

  belongs_to :user

  serialize :response, JSON

  has_one :vehicle_document, dependent: :destroy
end

VehicleDocument belongs_to: :Vehicle
class CreateVehicleDocuments < ActiveRecord::Migration
   def change
      create_table :vehicle_documents do |t|
         t.string :uuid, limit: 36, null: false, index: true
         t.integer :status, default: 0
         t.attachment :file
         t.belongs_to :vehicle, index: true
         t.timestamps null: false
      end
      add_index :vehicle_documents, :status
   end
 end

vehicle_document.rb : 
class VehicleDocument < ActiveRecord::Base
  audited

  include UuidConcern
  self.primary_key = :uuid

  belongs_to :vehicle
end

the problem is that I want to use a uuid for the vehicle id. I don't want to expose the real ID outside of my API.
how must I do this ? 
Do I have to do this in the migration file ? model ?
Should I not be doing this ? 
Thanks a lot

A:

Looks like your issue is with data modeling rather than Rails. Indeed, if you want to know how to do something in Rails, you must know what you want to do first!
So you have tables vehicles, and documents for short, with each record of documents keeping track (belonging to) a vehicle. You have two choices: use vanilla IDs, so regular associations will use the autoincrementing ids for tracking, which you don't necessarily need to expose via your API. 
Or, you can use the uuid for tracking the association in the database directly, but the column on documents must accomodate it. After all, you can't put an uuid in an integer column. So you could make documents.vehicle_id a uuid type rather than serial/integer. (Depending on your database system)
Looking at the documentation for t.references, leading to connection.add_reference, gives a hint as to what options are available:

:type
     The reference column type. Defaults to :integer.

So really, it's just a matter of changing your column definition to reflect the right data type.

