I have three models, Alarms, Sites, Networks. They are connected by belongs_to relationships, but they live in diferent databases
class Alarm < ActiveRecord::Base
establish_connection :remote
belongs_to :site, primary_key: :mac_address, foreign_key: :mac_address
end
class Site < ActiveRecord::Base
establish_connection :remote
belongs_to :network
end
class Network < ActiveRecord::Base
establish_connection :local
end
I wish to select all alarms belonging to a particular network. I can do this usng raw sql within a scope as follows:
scope :network_alarms, lambda { |net|
#need to ensure we are interrogating the right databases - network and site are local, alarm is remote
remote=Alarm.connection.current_database
local=Network.connection.current_database
Alarm.find_by_sql("SELECT network.* FROM #{local}.network INNER JOIN #{local}.site ON network.id = site.network_id INNER JOIN #{remote}.alarm on #{remote}.alarm.mac_address = site.mac_address WHERE site.network_id=#{net.id}")
}
and this works fine. However, this scope returns an array, so I can't chain it (for example, to use with_paginate's #page
method). So
Is there a more intelligent way of doing this join. I have tried using join
and where
statements, for example (this is one of many variations I have tried):
scope :network_alarms, lambda { |net| joins(:site).where("alarm.mac_address = site.mac_address").where('site.network_id=?', net) }
but the problem seems to be that the rails #join
is assuming that both tables are in the same database, without checking the connections that each table is using.