Ruby on Rails的:初始化对象随机GUID的"。新"? [英] Ruby On Rails: Initialize object with random GUID on ".new"?

查看:131
本文介绍了Ruby on Rails的:初始化对象随机GUID的"。新"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Ruby和放大器; Rails的......我们的目标是创建一个用户的主键类( ID 的)不是一个整数,而是一个GUID。我想我的东西设置的ActiveRecord正常结束(不自动生成的 ID 的类型列的整数的,但与列的 ID 的类型字符串的代替),并能生成一个GUID的功能。

唯一缺少的部分,在这一点上是越来越Rails的初始化一个新的用户的类具有的 ID 的使用* generate_guid *功能,我已经写了。

我的提问:如何让Rails的初始化的 ID 的一个随机GUID?例如...

  @user = User.new
@用户帐号
> AG5CE52#<  - 一个随机生成的GUID ...
 

同样重要的是要考虑到,如果一个实例的用户的是从数据库加载,它不应该改变的的id已经存在的用户的;当余用户被首次创建仅应产生一个GUID。

下面是一些code样品:<​​/ P>

模型

 类用户的LT;的ActiveRecord :: Base的
  before_create:generate_guid#这似乎并没有做任何事情(当我打电话User.new)

  set_primary_key:ID

  保护

  高清generate_guid
    开始
      ID = SecureRandom.urlsafe_base64
    结束而User.where(:ID =&GT; ID).exists?
    self.id = ID
  结束
结束
 

迁移

 类CreateUsers&LT; ActiveRecord的::迁移
  高清self.up
    CREATE_TABLE:用户{:ID =&GT;假}做| T |
      t.string:ID
      ...
    结束
    执行ALTER TABLE用户添加主键(id);
结束
 

建议是AP preciated!

  • 戴夫
解决方案

您可以使用 after_initialize 的回调,就是所谓的每一个类的对象实例化时。

 类用户的LT;的ActiveRecord :: Base的
   after_initialize:generate_guid,除非:GUID
   高清generate_guid
     self.guid =#您在这里的方法
   结束
 结束
 

您还可以设置这个字段是在迁移主键:

  CREATE_TABLE:用户,primary_key:GUID办| T |
 

然而

,你真的需要每次都创建一个GUID实例化一个对象吗?看来真的computationaly昂贵。正如有人评论的,予以警告,铁轨有时行为古怪,当你走出常见的模式和惯例......

I'm new to Ruby & Rails... the goal is to create a User class whose primary key (id) is not an integer, but a GUID. I think I have the ActiveRecord end of things setup properly (not auto-generating an id column with type integer, but with column id of type string instead), and a function that can generate a GUID.

The only missing piece at this point is getting Rails to initialize a new User class' with an id using the *generate_guid* function I had wrote.

My question: How do I get Rails to initialize id with a random GUID? For example...

@user = User.new
@user.id
> 'AG5CE52' # <-- A randomly generated GUID...

It's also important to consider that if an instance of User is loaded from the database, it shouldn't change the id of the already existing user; a GUID should only be generated when I user is being created for the first time.

Here are some code samples:

The model:

class User < ActiveRecord::Base
  before_create :generate_guid  # This doesn't seem to do anything (when I call User.new)

  set_primary_key :id

  protected

  def generate_guid
    begin
      id = SecureRandom.urlsafe_base64
    end while User.where(:id => id).exists?
    self.id = id
  end
end

The migration:

class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users, {:id => false} do |t|
      t.string :id
      ...
    end
    execute "ALTER TABLE users ADD PRIMARY KEY (id);"
end

Suggestions are appreciated!

  • Dave

解决方案

you can use the after_initialize callback, that is called every time an object of your class is instantiated.

 class User < ActiveRecord::Base
   after_initialize :generate_guid, unless: :guid
   def generate_guid
     self.guid = # your method here
   end
 end

you can also set this field to be a primary key in your migration:

create_table :users, primary_key: :guid do |t| 

however, do you really need to create a guid every time you instantiate an object ? It seems really computationaly expensive. And as someone commented, be warned that rails sometimes behaves weirdly when you get out of common patterns and conventions...

这篇关于Ruby on Rails的:初始化对象随机GUID的&QUOT;。新&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆