Wednesday, March 10, 2010

Ruby on Rails: Custom primary key in ActiveRecord

Replacing the default integer-based primary keys in model with a Custom primary-key.

The solution is to disable the id column and create a customized primary key column instead.

create_table :posts, :id => false do |t|
t.string :post_id, :limit => 36, :primary => true
end

In your Post model you should then set the name of this new primary key column.

class Post < ActiveRecord::Base
set_primary_key "uuid"
end

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.