[Rails] Re: compare items from 2 db

Dan Kirkwood
2006-06-15T17:31:21Z

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


--
Posted via http://www.ruby-forum.com/.