ruby on rails - When does ActiveRecord establish connections? -


in of rails applications, activerecord models seem establish db connections on initialization (e.g., when rails console), while in others connections seem established when reference model class or instantiate model object.

for example, went 1 application, opened rails console , wrote:

somemodel.connected? 

and returned false. went application, entered same command (for different model), , returned true. went third application , entered same command. time, waited moment , returned true, made me think connected? method triggered connection reason.

this difference in behavior doesn't seem related rails versions or contents of models. weird i've done in initializers, don't think so.

so when rails establish connections? or expected behavior?

additional info

i'll add doesn't seem connected? returns false because rails cannot connect database.

for example, in first application do:

somemodel.connected? # => false somemodel.table_exists? # or other command makes rails @ db # => true somemodel.connected? # => true 

answering own question:

whether database connection initialized during rails initialization process depends on whether activerecord::base.connection (not establish_connection) called during initialization process.

this can related rails version: example, in rails 3.2.13, "active_record.validate_explain_support" initializer makes call connection:

!activerecord::base.connection.supports_explain? 

in rails 3.2.14, call not made.

however, rails 3.2.14 may make call connection in "active_record.set_reloader_hooks" initializer. call may occur command

activerecord::base.clear_cache! 

although prepare callback runner doesn't seem call this...

i found gems (e.g., activeadmin) have initialization process call connection @ point.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -