establish_connection(spec = nil)
Instance Public methods
Establishes the connection to the database. Accepts a hash as input where
the :adapter
key must be specified with the name of a database
adapter (in lower-case) example for regular databases (MySQL, Postgresql,
etc):
1 2 3 4 5 6 7 | ActiveRecord::Base.establish_connection( adapter: "mysql" , host: "localhost" , username: "myuser" , password: "mypass" , database: "somedatabase" ) |
Example for SQLite database:
1 2 3 4 | ActiveRecord::Base.establish_connection( adapter: "sqlite3" , database: "path/to/dbfile" ) |
Also accepts keys as strings (for parsing from YAML for example):
1 2 3 4 | ActiveRecord::Base.establish_connection( "adapter" => "sqlite3" , "database" => "path/to/dbfile" ) |
Or a URL:
1 2 3 |
In case ActiveRecord::Base.configurations
is set (Rails
automatically loads the contents of config/database.yml into it), a symbol
can also be given as argument, representing a key in the configuration
hash:
1 | ActiveRecord::Base.establish_connection( :production ) |
The exceptions AdapterNotSpecified, AdapterNotFound and ArgumentError may be returned on an error.
Please login to continue.