Is it possible to use an optional ActiveRecord id column without auto-increment?

I would like to use a custom identifier (instead of the default automatic increments) for the Rails model. Basically, all identifiers will be iTunes store identifiers, which are simply long integers. Is it possible to disable the default auto-increment identifiers and require them to be set? These identifiers will also be used as foreign keys in other models.

+5
source share
2 answers

Something like that:

create_table :blah, {:id => false} do |t|
  t.int :my_custom_int_id
end
execute "ALTER TABLE blah ADD PRIMARY KEY (my_custom_int_id);"
+6
source

You can manually set the identifier before saving the model.

a = Model.new
a.id = 8888 #from itunes
a.save

itunes_id.

+2

All Articles