FaqRailsDatabaseAccess

How do I set up my Rails project to use my database?

Install the Required Software

First you have to get the right stuff, depending on whether you are using native Ruby or JRuby:

Native Ruby

If you are using a MySQL database, then Rails will work out-of-the-box. You have the option of installing the mysql gem, but doing so is not necessary. Some people say that the gem increases the speed of the database access.

For other database servers, consult the Ruby on Rails How-To pages. For example

See the Ruby on Rails Database Drivers page for the full list of supported drivers.

JRuby

Again, if you are using a MySQL database, then Rails will work out-of-the-box. You can also access a MySQL database through JDBC. For more information, see Faq Rails JRuby Database Access and Using Database Servers With JRuby.


Set Up Your Rails Project

In order to access a database from your Rails project, you must set up the configurations in the project's database.yml file. Typically, you supply an adapter, database name, username (and password if necessary) and host, similar to the configuration below:

  adapter: mysql
  database: my_development_database
  username: fred
  password: pfm
  host: localhost

When you create the project, the IDE automatically edits the database.yml based on the selections that you make in the third page of the wizard and the current Ruby interpreter (JRuby or native Ruby), as described below. If you use the defaults (MySQL and database names derived from the project name), you can skip page 3 altogether.

  • Configure Using Existing IDE Connections. Use this option if you want to select from database connections that have been registered with the IDE. If you have an existing database, but you have not registered it with the IDE, you can select New Database Connection from the drop-down list to add a connection to it. To create a new MySQL database, click Create DB. Type the User Name and Password in the MySQL Server Properties dialog box and click OK. Type the Database Name and click OK. Then type the connection parameters in the New Database Connection window.
  • Specify Database Information Directly. Here, you specify a Database Adapter and Database Name, type the User Name, and type the Password if one is required. This specified information is used for configuring the development database configuration only. The database names for the test and production database configurations are derived from the project name.

After Project Creation

After the IDE creates the project, you might need to edit the database.yml file to add your username and password. You might also need to set the database name, if the default setting is incorrect.

Note: You might need restart your web server after making changes to the database.yml so that the new settings get loaded.

For More Information

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo