简体   繁体   English

Rails3中的UUID

[英]UUIDs in Rails3

I'm trying to setup my first Rails3 project and, early on, I'm running into problems with either uuidtools , my UUIDHelper or perhaps callbacks. 我正在尝试设置我的第一个Rails3项目,并且在早期,我遇到了uuidtools ,我的UUIDHelper或者回调问题。 I'm obviously trying to use UUIDs and (I think) I've set things up as described in Ariejan de Vroom's article . 我显然正在尝试使用UUID和(我认为)我按照Ariejan de Vroom的文章中所描述的那样进行了设置。 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. 我已经尝试使用UUID作为主键,也只是一个补充字段,但似乎永远不会调用UUIDHelper

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. 我已经阅读了许多关于Rails3中回调和/或帮助器改变的提及,但我找不到任何可以告诉我如何调整的细节。 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: 如果出现错误“NoMethodError(UUID:Class的未定义方法`timestamp_create')”,则将set_uuid方法的内容更改为:

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

I believe this is necessary for more recent versions of the uuidtools gem. 我相信这对于更新版本的uuidtools gem来说是必要的。

Are you declaring another before_create method in your Image model? 您是否在Image模型中声明了另一个before_create方法? If so, you'll be overriding the one in the UUIDHelper module. 如果是这样,您将覆盖UUIDHelper模块中的那个。 You'll want to either declare the callback a different manner, or call super in the callback in your image model. 您需要以不同的方式声明回调,或者在图像模型的回调中调用super。

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 outline a working UUID example in this question: 我在这个问题中概述了一个有效的UUID示例:

Is COMB GUID a good idea with Rails 3.1 if I use GUIDs for primary keys? 如果我使用GUID作为主键,COMB GUID对Rails 3.1是一个好主意吗?

Obviously you can rewrite set_uuid any way you want--you don't have to use COMB GUIDs. 显然你可以set_uuid任何你想要的方式重写set_uuid - 你不必使用COMB GUID。

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 . 致谢:改编自https://github.com/boriscy/uuidrails3/blob/master/lib/uuid_helper.rb,使用UUID作为rails和polymorph关系中的主键时引用。 Also found an example at https://github.com/belucid/Recent-Updates/blob/884624e433cdffd63abd24b3bdb516a5d1596173/lib/uuid_helper.rb . 还在https://github.com/belucid/Recent-Updates/blob/884624e433cdffd63abd24b3bdb516a5d1596173/lib/uuid_helper.rb上找到了一个示例。

I also noticed you're missing the :id => false in your create_table . 我还注意到你在create_table缺少:id => false Check out the example from Ariejan's article a little more closely: 再仔细查看Ariejan文章中的示例:

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. 另外,我更喜欢UUIDTools::UUID.random_create.to_s到时间戳版本。 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

You might consider avoiding the use of string types to store your UUID, as it will make lookups super-slow. 您可以考虑避免使用字符串类型来存储UUID,因为它会使查找速度超慢。 There is an 'activeuuid' gem that looks promising (uses binary storage). 有一个'activeuuid'宝石看起来很有前途(使用二进制存储)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM