开发者

How can I set the default for a column as a GUID?

开发者 https://www.devze.com 2023-01-22 21:35 出处:网络
How can I set the RoR model\'s property (database column) to default for a column do generate a GUID in mysql?

How can I set the RoR model's property (database column) to default for a column do generate a GUID in mysql?

Does the column have to be a unique identitifier or can I have it be a st开发者_JS百科ring of length 36 also?


You may use uuid gem in your Rails project and a varchar (string) column in your table :

http://rubygems.org/gems/uuid

Then in your model :

class MyModel < ActiveRecord::Base
  before_save :generate_uuid

  protected
  def generate_uuid
    self.uuid ||= UUID.new.generate # Fill an UUID in uuid field only if the field is nil
  end
end
0

精彩评论

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