Connection_specification and adapter

Hi,

I wish to know, how those class work exactly, where
can I find the documentation? or can someone explain
to me how a connection is made via those class???

Thanks you very much!!!

Saiho


Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around

The connection adapters know how to connect to their respective
database servers. In the config/database.yml file you specify which
adapter (i.e. which database server) you want to use, and also
provide necessary parameters such as host, username, password. These
parameters vary by adapter.

Thus, you don’t have to make an explicit connection to the database
in your rails application. Rails will connect to the database using
the information you provided in config/database.yml

see http://api.rubyonrails.org for more details. Choose
ActiveRecord::ConnectionAdapters::WhateverAdapterYouNeedToUse for
adapter-specific details.

-Derrick S.

Hi,

Thanks you very much foir the information:) Do you
know who is the writter of these classes? I have some
question for him (or her)

Saiho

— Derrick S. [email protected] wrote:

Thus, you don’t have to make an explicit connection
to the database
in your rails application. Rails will connect to
the database using
the information you provided in config/database.yml

see http://api.rubyonrails.org for more details.
Choose

ActiveRecord::ConnectionAdapters::WhateverAdapterYouNeedToUse

I wish to know, how those class work exactly,



Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

If God really exists, I would like to
know what the dinosaurs have done to
deserve their extinction.

Water is unknown to fishes,
until they discover air.


Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around

Well, as far as i know the adapters were written by all different
people. For instance, I just finished writing one for OpenBase which
is sitting in the patch queue right now (ticket #3538).

I suppose your best bet is to post your questions to this list, and
possibly to the rails-core list and hope the author is listening :slight_smile:
You might also try asking around on IRC for the author of a specific
adapter.

-Derrick

Hi, Derrick

We are trying to connect to two diffferents database
within the same action. However, we doesn’t success,
because we can’t disconnect the first connection.
after some modification of the
connection_specification and the adapter, we are able
to “clear up” part of the connection. But each time we
try to connect to a new databse, Ruby-rails always
give us back the same adapter with the first
connection. Do you know how to stop proper a
connection???

we have clean up @@defined_connections and
@@active_connections, with “clear”, since those ara
instance of hash. But rails keep giving us back the
old adapter.

Do you know why?

Thanks you very much for your help

Saiho

— Derrick S. [email protected] wrote:

is listening :slight_smile:

wrote:
username,

see http://api.rubyonrails.org for more details.
Choose

ActiveRecord::ConnectionAdapters::WhateverAdapterYouNeedToUse

I wish to know, how those class work exactly,

Do You Yahoo!?


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around

I’m certainly no expert at this, as I’ve not even tried it myself.
However, I do know that you can specify a class-specific database
connection. Using this feature, I assume you could create two
models, say MyDataInPostgreSQL and MyDataInMySQL. Then, you could
use these two models in the same controller action and each model
would be creating it’s own class-specific connection to the database.

Exactly how to do it? I have no idea (sorry)…any experts out there?

-Derrick

I was just looking at the ActiveRecord::Base api. It looks like to
implement this you just need to overload the establish_connection
method in your model. If you don’t have this method in your model,
then the version in ActiveRecord::Base is used, which will connect to
the database in your config/database.yml file. By placing a method
named establish_connection in your model, you can have just that
model and it’s children use another database.

Hope that helps.

-Derrick S.

Perhaps we should review your design. Why do you need the same model
to access two different databases? Are you trying to move the data
between databases? What advantage are you gaining with this design?

If you’re worried about duplication of code between two models that
are schematically identical, but point to two different database,
then perhaps you could try subclassing your model. For instance:

class MyModel < ActiveRecord
… connects to default database …
… all my business logic, etc …
end

class MyOtherModel < MyModel
… inherits all business logic from MyModel,
but overrides the establish_connection method
to point at another database …
end

-Derrick

Hi, Derrick,

You are absolutely right about building two models
using in the same action. It works perfectly, I can
connect to two different databases without any
problem.

However in our actual case, I have to create one model
(a generic one) then try to connect it to two
differents database. the catch is no matter how we try
to reset the connection, Rails keep giving us back the
old adapter… any idea why?

Thanks again for the infomation!!!:slight_smile:

Saiho

— Derrick S. [email protected] wrote:

would be creating it’s own class-specific connection

Hi, Derrick
time we
old adapter.

#3538).

some
their

parameters vary by adapter.
see http://api.rubyonrails.org for more
details.
Choose

ActiveRecord::ConnectionAdapters::WhateverAdapterYouNeedToUse

I wish to know, how those class work exactly,

[email protected]

[email protected]

http://lists.rubyonrails.org/mailman/listinfo/rails


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around

Hi Derrick

Thanks again for the information, What we are trying
to do is to create a runtime generic
ActiveRecord::base. One aspect that we like to have is
able to change/read/write a database whenever it is
necessary. Actually, when you want to connect to a
database. you have to know where it is, which table
you want to read/write. so you have to predefine your
database connection in database.yml and predefine the
table-model class with ActiveRecord::Base.

So what we are trying to do, is to create in runtime a
generic class that will allow us to connect to a not
predifine database and tables. We have really sucess
to create a generic runtime class, we are able to
change/read/write tables within the same database.
However when we try to change the using database. We
are not able to this within the same action. We are
able to change database in two actions, but within the
same action, We don’t know why Rails keep giving us
back the old database adapter, even we have “clean up”
everything we “know”.

We suppect there is a “cache” of objects somewhere, in
Ruby interpreter or in Rails. Instead of return the
new adapter, it picks up the one for some unknown
reason… we are still experimenting…

Do you have any idea?

Thanks you very much once again:)!!!

Saiho


Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around