Reputation: 4376
I have 4 locations that I pull data in from a legacy system.
I pull 3 of the locations into one model (because I load the data in the same table).
The 4th location has to be in a separate database.
I am wondering if there is a way to combine the 2 in a model or 2 models in rails.
Here is what I currently have:
class Fyf003 < ActiveRecord::Base
establish_connection "turnkey"
end
and the other one is:
class WccFyf003 < ActiveRecord::Base
establish_connection "turnkey_wcc"
def self.table_name() "fyf003" end
end
I am guessing this is not the correct way to do this, which is why i'm here.
The table layouts and names are identical.
If you need anymore info or can point me in the right direction, that would be great.
Thank you for any help.
Upvotes: 1
Views: 514
Reputation: 4376
I figured out I could just use:
sql = "SELECT * FROM turnkey.fyf003
UNION ALL
SELECT * FROM turnkey_wcc.fyf003"
lot_data = Fyf003.find_by_sql(sql)
If someone has a better idea, I am all for it.
EDIT: I have found out since posting this that you can do the following if the data is the same:
data1 = Fyf003.all
data2 = WccFyf003.all
all_data = data1+data2
It just combines the arrays, and you still have seperate objects, but you can use .collect(&:field) on it and have it work.
Still looking for the best solution if someone has it.
Upvotes: 1
Reputation: 8757
I'm guessing you're using multiple models to map to one table. If that's the case, in your models;
set_table_name 'table_name'
set_primary_key 'primary_key'
Upvotes: 0