2013-10-03 53 views
3

我需要實現activeuuid gem以使UUID代替默認的Rails ID。我們可以實現它來創建新的遷移爲:使用activeuuid gem for rails中的現有數據庫

class CreateStudents < ActiveRecord::Migration 
    def change 
    create_table :students, :id => false do |t| 
     t.uuid :id, :primary_key => true 
     t.string :name 
     t.string :email 

     t.timestamps 
    end 
    end 
end 

而在模型中,我們包括ActiveUUID :: UUID爲:

class Student < ActiveRecord::Base 
    attr_accessible :email, :name 
    include ActiveUUID::UUID 
end 

現在我已經有一個數據庫,這樣我怎麼能實現activeuuid寶石有UUID而不是現有數據庫的默認Rails ID? 需要對所有遷移進行更改或做什麼? 在這方面需要幫助。感謝

+0

好問題,我正面臨同樣的問題!你介意分享你最終做的事情嗎? – oregontrail256

回答

0

的UUID存儲爲二進制領域瓦特/ 16的位置,因爲我在這裏找到:https://github.com/jashmenn/activeuuid/blob/master/lib/activeuuid/patches.rb#L62

它爲我(沒有記錄現有的表):

def change 
    reversible do |dir| 
    change_table :payments do |t| 
     dir.up { t.change :id, :binary, limit: 16, :primary_key => true } 
     dir.down { t.change :id, :integer } 
    end 
    end 
end 

不要忘了添加這些行到您的型號還有:

include ActiveUUID::UUID 
natural_key :at_least_one_field_here 

在GitHub庫更多信息:https://github.com/jashmenn/activeuuid/