ringe
ringe

Reputation: 812

Multiple tables behind single ActiveRecord model

I am working with two legacy windows applications that we integrate our Ruby on Rails app with. Their databases are a horrible mess.

In each of these I have two different tables representing an order. For one database I have these ActiveRecord models:

CustomerOrder
CustomerOrderCopy

The first table is used for orders in progress. The second table is the archive.

Often I have to look in both tables to find an order.

Does anyone have a suggestion to how I can make one ActiveRecord model represent both tables? Meaning, if the order is not found in one, use the other.

update

I use the following to reference an order across the tables, from another model:

has_one :external_order, foreign_key: :OrderNo, primary_key: :external_ordernumber,
  class_name: Visma::CustomerOrder
has_one :visma_order_copy, foreign_key: :OrderCopyNo, primary_key: :external_ordernumber,
  class_name: Visma::CustomerOrderCopy

# Return the external order in Visma from it's current table
def visma_order
  external_order || visma_order_copy
end

I am asking for thoughts on how to have one model represent both tables.

Upvotes: 1

Views: 816

Answers (1)

Breno Perucchi
Breno Perucchi

Reputation: 893

Do you have to create a logicy for this

maybe something like this

#customer_order.rb

def self.search_order(param)
  self.where(id: param).first || CustomerOrderCopy.where(id: param).first
end

CustomerOrder.search_order(1)

Upvotes: 1

Related Questions