Forum: Ruby on Rails compare items from 2 db

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0eed2d21bf6272296cf42c1e01a7cf7d?d=identicon&s=25 Dan K. (dangogh)
on 2006-06-14 02:44
I've dug thru docs, recipes, wiki, but haven't found anything quite like
what I'm trying to do..

I have 2 databases with the same tables and same models representing
them.  I want to get an item (of same name) from the 2 dbs for
comparison.  Now, each example of connecting to multiple dbs I've seen
(including from Rails Recipes and the wiki) assumes that you want
different models accessing the different databases.  How can I have the
same model access 2 different dbs at the same time?

I've looked at trying to subclass each model so the subclass can access
an alternate database (I'm using single-table inheritance, so that path
is out.).

I've also thought of using an abstract base class (similar to the
LegacyBase class mentioned in the wiki), but that won't work because
then *all* items would access the alternate db..

Any pointers appreciated..  -dan
0eed2d21bf6272296cf42c1e01a7cf7d?d=identicon&s=25 Dan K. (dangogh)
on 2006-06-15 19:31
I got this to work partially,  by defining a parallel class in a
separate module .  A contrived example:

class Person
  include person_methods
  has_one :address
  validates_presence_of :name
  validates_presence_of :type
end

class Employee < Person
  include employee_methods
  validates_form_of :employee_id => /\d+/
end

module ToCompare
  class Person < Object::Person
    include person_methods
    has_one :address
    validates_presence_of :name
    validates_presence_of :type

    establish_connection :db_to_compare
  end
  class Employee < Person
    include employee_methods
    validates_form_of :employee_id => /\d+/
  end
end


Two issues:  1. duplication.. I used modules to keep from duplicating
the method definitions,   but how can I avoid duplicating the
relationships and validations?
2. p=ToCompare::Person.find(:first)
   p.connection -- shows it's connecting to :db_to_compare
   p.address.connection -- shows :development
How to make the related items connect to the same db?


Dan Kirkwood wrote:
> I've dug thru docs, recipes, wiki, but haven't found anything quite like
> what I'm trying to do..
>
> I have 2 databases with the same tables and same models representing
> them.  I want to get an item (of same name) from the 2 dbs for
> comparison.  Now, each example of connecting to multiple dbs I've seen
> (including from Rails Recipes and the wiki) assumes that you want
> different models accessing the different databases.  How can I have the
> same model access 2 different dbs at the same time?
>
> I've looked at trying to subclass each model so the subclass can access
> an alternate database (I'm using single-table inheritance, so that path
> is out.).
>
> I've also thought of using an abstract base class (similar to the
> LegacyBase class mentioned in the wiki), but that won't work because
> then *all* items would access the alternate db..
>
> Any pointers appreciated..  -dan
This topic is locked and can not be replied to.