I’m looking at possibly having to define a has_many relationship across
two databases (possibly even two databases on different servers). To be
clear, I mean model A lives in database X and it’s related to model B
that lives in database Y. A has_many B’s.
It appears that if I want to do this, I have two options:
- I could provide custom finder_sql A’s has_many (and B’s belongs_to)
declaration that would handle the cross-DB join for me. This implies
DB-specific SQL syntax depending on how the DB in question handles
- I could define a DB-specific synonym from DB X to DB Y to allow the
association backing table for model B to appear to be local in the same
DB as model A. This approach basically puts the complexity in the DB
instead of the finder_sql.
I wanted to look into pursuing option #1 for now.
I’m under the impression that I should be able to dynamically construct
the cross-DB finder SQL that I need based on calls that I can make to
the Connection object in ActiveRecord::Base. I figure I can pull out
the server, db name, and possibly even port from the connection
specification to construct appropriate SQL.
Has anyone done this or tried to do it?