Ruby on Rails development from scratch (43)-ActiveRecord Foundation (Connection database)

Source: Internet
Author: User
Tags mysql mysql database ruby on rails

An active record abstracts the concept of a database connection and helps the application to handle the details of the underlying database links, in lieu of the active record using a generic invocation to delegate the details to a set of database adapters.

You can use the Establish_connection () method to make a connection, the following example creates a MySQL database connection, the name of the database is Railsdb, The server's host name is dbserver.com, the user name is Railsuser, and the password is RAILSPW.

Activerecord::base.establish_connection (
: Adapter => "MySQL"
: Host => "dbserver.com",
:d atabase => "Railsdb",
: Username => "Railsuser",
:p assword => "RAILSPW"
)

The Active record supports Db2,mysql,oracle,postgres,sqlserver and Sqllite, each of which has a small difference in the parameters of the link, and the following table lists the commonly used parameters:

Note that the Oracle adapter name is OCI.

The database connection is associated with the model class, and each class inherits the link from the parent class, activerecord::base as the parent of all active record classes, setting up the database connection here to link all the active records classes, and of course, if necessary, You can also make a replication (override) Link configuration.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.