开发者

UUIDs in Rails3

开发者 https://www.devze.com 2022-12-23 10:40 出处:网络
I\'m trying to setup my first Rails3 project and, early 开发者_StackOverflowon, I\'m running into problems with either uuidtools, my UUIDHelper or perhaps callbacks.I\'m obviously trying to use UUIDs

I'm trying to setup my first Rails3 project and, early 开发者_StackOverflowon, I'm running into problems with either uuidtools, my UUIDHelper or perhaps callbacks. I'm obviously trying to use UUIDs and (I think) I've set things up as described in Ariejan de Vroom's article. I've tried using the UUID as a primary key and also as simply a supplemental field, but it seems like the UUIDHelper is never being called.

I've read many mentions of callbacks and/or helpers changing in Rails3, but I can't find any specifics that would tell me how to adjust. Here's my setup as it stands at this moment (there have been a few iterations):

# migration
class CreateImages < ActiveRecord::Migration
  def self.up
    create_table :images do |t|
      t.string :uuid, :limit  => 36
      t.string :title
      t.text :description

      t.timestamps
    end
  end
  ...
end

# lib/uuid_helper.rb
require 'rubygems'
require 'uuidtools'

module UUIDHelper
  def before_create()
    self.uuid = UUID.timestamp_create.to_s
  end
end

# models/image.rb
class Image < ActiveRecord::Base
  include UUIDHelper

  ...
end

Any insight would be much appreciated.

Thanks.


If you get an error "NoMethodError (undefined method `timestamp_create' for UUID:Class)", then change the contents of the set_uuid method to:

self.uuid = UUIDTools::UUID.timestamp_create().to_s

I believe this is necessary for more recent versions of the uuidtools gem.


Are you declaring another before_create method in your Image model? If so, you'll be overriding the one in the UUIDHelper module. You'll want to either declare the callback a different manner, or call super in the callback in your image model.

Edit: Maybe change the helper to look something like this:

module UUIDHelper
  def self.included(base)
    base.class_eval do
      before_create :set_uuid

      def set_uuid
        self.uuid = UUID.timestamp_create.to_s
      end
    end
  end
end


I also noticed you're missing the :id => false in your create_table. Check out the example from Ariejan's article a little more closely:

create_table :posts, :id => false do |t|
  t.string :uuid, :limit => 36, :primary => true
end

Additionally, I prefer the UUIDTools::UUID.random_create.to_s to the timestamp version. YMMV.


I had to specify the primary key in my model to make it work at the controller level.

class Image < ActiveRecord::Base
  include UUIDHelper
  set_primary_key :uuid

  ...

end


I outline a working UUID example in this question:

Is COMB GUID a good idea with Rails 3.1 if I use GUIDs for primary keys?

Obviously you can rewrite set_uuid any way you want--you don't have to use COMB GUIDs.

Credits: adapted from https://github.com/boriscy/uuidrails3/blob/master/lib/uuid_helper.rb referenced in using UUID as primary key in rails and polymorph relationships. Also found an example at https://github.com/belucid/Recent-Updates/blob/884624e433cdffd63abd24b3bdb516a5d1596173/lib/uuid_helper.rb.


You might consider avoiding the use of string types to store your UUID, as it will make lookups super-slow. There is an 'activeuuid' gem that looks promising (uses binary storage).

0

精彩评论

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