'How do I make ActiveRecord search multiple tables (of similar schema) by default?

I have a very large table Metrics with a primary integer key named id.

The primary key is 32 bit integer and is about to overflow. My attempted solution to this problem is this:

  1. Make a new table with (almost) the same schema, but instead a 64 bit integer for ID.
  2. Set self.table_name = "metrics_with_bigints" on Metric Model
  3. Create a record in metrics_with_bigints table with (31 ^^ 2) + 1 ID

This works fine for NEW records. However, I want to be able to call Metrics.find(1) and still have it return the record.

This table is APPEND ONLY and records are not ever modified.

I think that I want to UNION ALL the 2 tables by default but am having trouble solving this.

My thought is:

class Metric < AR::Base
  def self.find_by_sql(*args)
    sql = args.first
    # do something here that unions the 2 tables?
    super
  end
end

I am unsure if this approach is viable.



Solution 1:[1]

I believe you can change the type of primary key. and change it to UUID. It is 128 bit Number with several added advantages. follow this -> https://andycroll.com/ruby/choose-uuids-for-model-ids-in-rails/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Prateek Vyas