views:

25

answers:

1

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?

+1  A: 

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
slainer68