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 string of length 36 also?
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 string of length 36 also?
You may use uuid gem in your Rails project and a varchar (string) column in your table :
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