0

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

Mike W
  • 391
  • 4
  • 14
  • http://stackoverflow.com/questions/750413/altering-the-primary-key-in-rails-to-be-a-string – Ilya Aug 22 '16 at 13:38
  • this doesn't explain how to define the FK but just the primary. Defining the uuid as the primary is not the issue. The issue is making sure that the vehicle_id column in the database is a string, and not an integer – Mike W Aug 22 '16 at 14:11
  • Please post your models so we can see the associations as you have them defined. It may be that simply adding `foreign_key: :uuid` to the appropriate associations might do what you're looking for, but it's impossible to tell w/o seeing that code. – jaydel Aug 22 '16 at 14:54
  • 2
    To the person who voted to close this as a duplicate. At least paste the URL to the question it is a duplicate of into a comment. As it stands, whoever voted for the close specified this as the duplicate: http://stackoverflow.com/questions/17787586/how-to-set-up-custom-string-foreign-key-in-rails-4 – jaydel Aug 22 '16 at 14:55
  • I just add the models. thanks jaydel. What I see is that when I run the migration it creates the vehicle_id in the VehicleDocument has an integer and not a string – Mike W Aug 22 '16 at 14:59

1 Answers1

2

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.

Jonathan Allard
  • 18,429
  • 11
  • 54
  • 75
  • great, thanks a lot. I knew it was something to do with the DB model, rather than rails but since most of the time everything is doable through rails I just wanted to make sure. anyway thank a lot Jonathan, it worked – Mike W Aug 23 '16 at 08:46