我需要为我的rails应用程序生成uuid。我有什么选择(宝石)?(复制)

时间:2022-07-31 20:56:16

This question already has an answer here:

这个问题已经有了答案:

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。

4 个解决方案

#1


190  

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

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

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

See other possible formats here.

请参阅这里的其他可能格式。

#2


10  

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

#3


3  

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

执行<-SQL创建表some_items (id uuid主键默认uid_generate_v1());SQL

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

在Rails 4中这样做可能更好。

#4


2  

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标准库使用UUID,并举例说明rails 3。倍和4.倍

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

创建usesguid。库/usesguid中的rb文件。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 -

在配置/应用程序中添加以下行。rb加载文件-

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

Create migration script for UUID function as mentioned below to -

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

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

铁路4的其他选择

#1


190  

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

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

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

See other possible formats here.

请参阅这里的其他可能格式。

#2


10  

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

#3


3  

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

执行<-SQL创建表some_items (id uuid主键默认uid_generate_v1());SQL

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

在Rails 4中这样做可能更好。

#4


2  

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标准库使用UUID,并举例说明rails 3。倍和4.倍

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

创建usesguid。库/usesguid中的rb文件。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 -

在配置/应用程序中添加以下行。rb加载文件-

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

Create migration script for UUID function as mentioned below to -

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

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

铁路4的其他选择