I read the example in Peak Obsession,
it show us how to connect to other database, each time
we start a new action, however, this doesn’t work
while you try to connect to two different database
within the same action.
SO I wonder is it possible to bind to two or more
database within the same action???
Thanks you very much
Saiho
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
Actually, we try to create a “generic”
activerecord::base class that can be bind to different
databases within the same action. It means the same
subclass of activerecord::base can link(and connect)
to different tables of different database at runtime.
Insteads of predefine the (model)class with the
scarffold. Here is an example of what we are trying to
do:
Leaving things pretty much as you had them, this works for me. I
removed some unnecessary statemetnts. Also, I changed the database to
mysql and am running it as a script for testing.
Here’s the code:
----script/gentest----
#!/usr/bin/env ruby
require File.dirname(FILE) + ‘/…/config/boot’
----saiho2.sql----
CREATE TABLE table2 ( id int(11) NOT NULL auto_increment, bar varchar(10) default NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
----A sample run----
id
foo
id
bar
#<GenTableAs:0x23d9250 @attributes={“id”=>“1”, “bar”=>“baz”}>
Honestly, I wouldn’t want to re-establish this connection all the time,
but I’m not really sure how dynamic this is going to be. For example,
is it 2 databases or will there be 100 databases? If you are going to
be reusing connections, you should stash them somewhere and then make
them available with connection=.
and when I execute the same code inside a
controller/action I got the following error:
ActiveRecord::StatementInvalid in Gentest#index3
ERROR: relation “gen_table_as” does not exist
: SELECT a.attname,
format_type(a.atttypid, a.atttypmod), d.adsrc,
a.attnotnull
FROM pg_attribute a LEFT JOIN pg_attrdef
d
ON a.attrelid = d.adrelid AND a.attnum
= d.adnum
WHERE a.attrelid =
‘gen_table_as’::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
RAILS_ROOT: .
Application Trace | Framework Trace | Full Trace
ny the way, which version of Ruby-rails that you are
using?
I’m using
The using version ruby-rails are :
Rails 0.14.2 and ruby 1.8.3 (2005-09-21)
[x86_64-linux]
We are running on Linux KDE 3.4, and postgresql