Ruby-on-rails 我需要为我的 rails 应用程序生成 uuid。我有哪些选择(宝石)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18228324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:31:54  来源:igfitidea点击:

I need to generate uuid for my rails application. What are the options(gems) I have?

ruby-on-railsrubyruby-on-rails-3guiduuid

提问by Virtual

I use Rails 3.0.20 and ruby 1.8.7 (2011-06-30 patchlevel 352)

我使用 Rails 3.0.20 和 ruby​​ 1.8.7 (2011-06-30 patchlevel 352)

Please suggest me the best plugin to generate guid.

请建议我最好的插件来生成 guid。

回答by apneadiving

There are plenty of options, I recommend not to add additional dependencies and use SecureRandomwhich is builtin:

有很多选项,我建议不要添加额外的依赖项并使用SecureRandom内置的:

SecureRandom.uuid #=> "1ca71cd6-08c4-4855-9381-2f41aeffe59c"

See other possible formats here.

在此处查看其他可能的格式。

回答by Hasan Iqbal

The first thing I would suggest is that please upgrade your ruby and rails version.

我建议的第一件事是请升级您的 ruby​​ 和 rails 版本。

A very good way of generating guid is SecureRandom, which is a ruby module. With easy usage.

生成 guid 的一个很好的方法是SecureRandom,它是一个 ruby​​ 模块。使用方便。

require 'securerandom'
guid = SecureRandom.hex(10) #or whatever value you want instead of 10

回答by riley

I would suggest using PostgreSQL and using the uuid column built in, it autogenerates UUID based on type you create the column.

我建议使用 PostgreSQL 并使用内置的 uuid 列,它会根据您创建列的类型自动生成 UUID。

Example in Rails 3 migration

Rails 3 迁移示例

execute <<-SQL CREATE TABLE some_items (id uuid PRIMARY KEY DEFAULT uuid_generate_v1()); SQL

execute <<-SQL CREATE TABLE some_items (id uuid PRIMARY KEY DEFAULT uuid_generate_v1()); SQL

Might be a better way to do this in Rails 4.

可能是在 Rails 4 中执行此操作的更好方法。

回答by Rameshwar Vyevhare

Please see in detail, how to use securerandom ruby standard library to use UUID with example in rails 3.X and 4.X

请详细查看如何使用securerandom ruby​​标准库在rails 3.X和4.X中以示例使用UUID

create usesguid.rb file in your lib/usesguid.rb and paste below code in that -

在你的 lib/usesguid.rb 中创建 usesguid.rb 文件并粘贴下面的代码 -

require 'securerandom'

module ActiveRecord
  module Usesguid #:nodoc:
    def self.append_features(base)
      super
      base.extend(ClassMethods)  
    end

    module ClassMethods
      def usesguid(options = {})
        class_eval do
          self.primary_key = options[:column] if options[:column]
          after_initialize :create_id
          def create_id
            self.id ||= SecureRandom.uuid
          end
        end
      end
    end
  end
end
ActiveRecord::Base.class_eval do
  include ActiveRecord::Usesguid
end

add following line in your config/application.rb to load file -

在 config/application.rb 中添加以下行以加载文件 -

require File.dirname(__FILE__) + '/../lib/usesguid'

Create migration script for UUID function as mentioned below to -

为 UUID 函数创建迁移脚本,如下所述 -

class CreateUuidFunction < ActiveRecord::Migration
  def self.up
    execute "create or replace function uuid() returns uuid as 'uuid-ossp', 'uuid_generate_v1' volatile strict language C;"
  end

  def self.down
    execute "drop function uuid();"
  end
end

Here is example for contact migration, how we can use it -

这是联系人迁移的示例,我们如何使用它 -

class CreateContacts < ActiveRecord::Migration
  def change
    create_table :contacts, id: false do |t|
      t.column :id, :uuid, null:false 
      t.string :name
      t.string :mobile_no

      t.timestamps
    end
  end
end

Final how to use into your model

最终如何在您的模型中使用

class Contact < ActiveRecord::Base
  usesguid

end

This will help you to configure UUID for your rails application.

这将帮助您为 Rails 应用程序配置 UUID。

This can be useful for Rails 3.0, 3.1, 3.2 and 4.0 as well.

这对 Rails 3.0、3.1、3.2 和 4.0 也很有用。

Please let me know If you have any issue while using it, so simple!

如果您在使用它时遇到任何问题,请告诉我,就这么简单!

Other options for Rails4 here

Rails4 的其他选项在这里