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
精彩评论