Rails: can I use polymorphic links with non-integer primary keys?

I have a database that uses the UUID as primary keys, for example:

create_table "my_table", :id => false, :force => true do |t|
 t.string "id", :limit => 36
end

However, when I try to use: links for foreign keys for this table, it generates whole columns for the identifier. Can: give instructions for working with a non-integer identifier? My migration for the reference table is this:

create_table "child_table" :id => false, :force => true do |t|
 t.string "id", :limit => 36
 t.references :my_table
end

I know that I can simply manually create columns :my_table_idand :my_table_type, but I wonder if it is possible to :referencesdo my magic in these circumstances so that I do not have to process the identifier + enter explicitly throughout the code.

+5
2

, references .

, references, , . IMO UUID . , , .

+3

A: Rails 4.2

t.references :car, type: :uuid, index: true

:

def change
  enable_extension 'uuid-ossp'
  create_table :cars, id: :uuid do |t|
    t.integer :seats
    # And other car-specific things
  end
  create_table :wheels do |t|
    t.references :car, type: :uuid, index: true
    t.integer :radius
    # And other wheel-specific things
  end
end

: https://github.com/rails/rails/pull/16231

+15

All Articles