简体   繁体   English

Ruby on Rails:如何生成应用程序范围的序列?

[英]Ruby on Rails : how to generate an application-wide sequence?

I am developing on RoR 4, with Oracle, PostGreSQL and MSSQL as target databases. 我正在开发RoR 4,Oracle,PostGreSQL和MSSQL作为目标数据库。

I am building a hierarchy of 4 objects, for which I need to display parent-child relationships through the same query whatever level I start from. 我正在构建一个包含4个对象的层次结构,我需要通过相同的查询显示父子关系。 Not easy to figure out, but the hint is that none of the object should have identical IDs . 不容易弄清楚,但提示是没有任何对象应该具有相同的ID

The issue here is that rails maintains a dedicated sequence for each object, so duplicated IDs will appear for sure. 这里的问题是rails为每个对象维护一个专用序列,因此肯定会出现重复的ID。

How can I create a sequence to fill a unique_id field which remains unique for all my data ? 如何创建一个序列来填充unique_id字段,该字段对于我的所有数据都是唯一的?

Thanks for your help, 谢谢你的帮助,

Best regards, 最好的祝福,

Fred 弗雷德

I finally found this solution: 我终于找到了这个解决方案

1 - create a sequence to be used by each of concerned objects 1 - 创建每个相关对象使用的序列

class CreateGlobalSequence < ActiveRecord::Migration
  def change
    execute "CREATE SEQUENCE global_seq INCREMENT BY 1 START WITH 1000"
  end
end

2 - Declare this sequence to be used for identity columns in each of concerned models 2 - 声明此序列用于每个相关模型中的标识列

class BusinessProcess < ActiveRecord::Base

self.sequence_name = "global_seq"
...
end


class BusinessRule < ActiveRecord::Base

self.sequence_name = "global_seq"
...
end

and so on. 等等。 It works fine. 它工作正常。

Rails is great ! Rails太棒了!

Thanks for your help, and best regards, 感谢您的帮助和最诚挚的问候,

Fred 弗雷德

Id column for each table is unique identifier for each table record. 每个表的Id列是每个表记录的唯一标识符。 It will not make any impact on other table Id column. 它不会对其他表Id列产生任何影响。

Don't know why you need this. 不知道你为什么需要这个。 But you can achieve it by some extent. 但是你可以在某种程度上实现它。 Like below : 如下所示:

class CreateSimpleModels < ActiveRecord::Migration
  def self.up
    create_table :simple_models do |t|
      t.string :xyz
      t.integer :unique_id
      t.timestamps
    end
    execute "CREATE SEQUENCE simple_models_unique_id_seq OWNED BY
simple_models.unique_id INCREMENT BY 1 START WITH 100000"
  end

  def self.down
    drop_table :simple_models
    execute "DELETE SEQUENCE simple_models_unique_id_seq"
  end
end

But after 100000 record in db it will again going to similar for other model. 但是在db中100000记录后,它将再次与其他型号相似。

The default id column has the identity attribute, which is stored per-table. 默认id列具有identity属性,该属性存储在每个表中。 If your models fit the bill for Single Table Inheritance you'd be able to define a custom id attribute on the base class. 如果您的模型符合单表继承的帐单,您就可以在基类上定义自定义ID属性。 In your case since you said it's a hierarchy that might be the way to go. 在你的情况下,因为你说它是一个层次结构,可能是要走的路。

The harder? 更难? (STI is a bit to digest but very powerful) way of doing this involves what I'm working on this similar issue with a shared PAN (Private Account Number in this system) in a shared namespace. (STI有点消化,但非常强大)这样做的方式涉及我正在使用共享命名空间中的共享PAN(此系统中的专用帐号)处理此类似问题。

class CreatePans < ActiveRecord::Migration
  def change
    create_table :pans do |t|
      t.string :PAN
      t.timestamps
    end
  end
end

class AddPanIdToCustomers < ActiveRecord::Migration
  def change
    add_column :customers, :pan_id, :integer
  end
end

The first migration will add the ID table, the second adds the foreign key to the customers table. 第一次迁移将添加ID表,第二次将外键添加到customers表。 You'll also need to add the relationships to the models has_many :pans and belongs_to :customers . 您还需要将关系添加到模型has_many :pansbelongs_to :customers You can then refer to their identity by the :pan_id attribute (however you name it). 然后,您可以通过:pan_id属性引用它们的标识(但是您可以将其命名)。 It's a roundabout way of doing things, but in my case business requirements force it - hacky as it is. 这是一种迂回的做事方式,但就我而言,业务要求迫使它 - 实际上是hacky。

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

相关问题 Rails + SSL:每个控制器还是应用程序范围? - Rails + SSL: Per controller or application-wide? 在Rails 3.0 / 3.1中,我应该如何缓存应用程序范围内所需的不变数据? - In Rails 3.0/3.1 how should I cache unchanging data that's needed application-wide? Ruby将“ Model.all”保存到应用程序范围的变量中 - Ruby saving “Model.all” into application-wide variable 应用程序范围内的jQuery脚本 - application-wide jquery script Redmine插件应用程序范围的权限 - Redmine plugin application-wide permissions 可以从Rails中的控制器获取和设置的应用程序范围的全局变量 - Application-wide global variable that can be get and set from controllers in Rails 在Rails中定义应用程序范围的部分及其变量的正确方法是什么 - What is the correct way to define an application-wide partial and its variables in rails 通过Web表单使用户可以访问应用程序范围的设置 - Make application-wide settings accessible to the user via web forms 在link_to生成的链接中添加应用程序范围的默认参数 - Adding an application-wide default parameter to link_to generated links 渲染应用程序范围的视图以及Ruby on Rails中的特定视图 - Render application wide view plus specific view in Ruby on Rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM