开发者

Custom non auto-incrementing ActiveRecord "id" column possible?

I'd like to be able to use a custom id (inst开发者_如何学运维ead of the auto-incrementing default ones) for a Rails model. Basically, all the ids will be iTunes store ids, which are just long integers. Is it possible to turn off the default auto-incrementing ids and require that one be set? These ids will also be used as foreign keys in other models.


Something like this:

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);"


You can manually set the id before you save the model.

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

However, you should consider a separate field called itunes_id instead of this approach.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜