Home arrow Ruby-on-Rails arrow Database for a Ruby-on-Rails Application

Database for a Ruby-on-Rails Application

In this second part of a four-part article series on building a basic Ruby-on-Rails application, you will learn how to create both a database and the event model. This article is excerpted from chapter three of the book Beginning Rails: From Novice to Professional, written by Jeffery Allan Hardy, Cloves Carneiro Jr. and Hampton Catlin (Apress; ISBN: 1590596862).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 5
February 08, 2011
  1. · Database for a Ruby-on-Rails Application
  2. · Creating the Event Model

print this article

Database for a Ruby-on-Rails Application
(Page 1 of 2 )

Creating the Project Databases

To create a new database, you can use your favorite database administration tool, or you can use the mysqladminprogram from the command line. Since we’re command-line junkies, we’ll do the latter.

$ mysqladmin -uroot create events_development

We give themysqladmin program two arguments: an action (create) and the name of the database we want to create. That should be sufficient to create a new database calledevents_development.

Note  Depending on how your environment is set up, you might not need to specify the username under You might also need to supply a password, which you can do using the-poption. Here, we’re using MySQL’s defaultrootuser (with no password), which should work on most installations.

While we’re only concerned with the development environment at this time, it won’t hurt to create the other databases while we’re at it. Go ahead and create two more databases, one each for the test and production environments.

$ mysqladmin -uroot create events_test
$ mysqladmin -uroot create events_production

You can confirm the creation of the database using themysqlshowprogram to take a look at all the MySQL databases currently on your system.

$ mysqlshow


|       Databases           |
| information_schema        |
| events_development        |
| events_test               |
| events_production         |
| mysql                     |
| test                      |


For most MySQL installations, the default connection parameters indatabase.yml(a username ofroot and no password) will work out of the box. If you’ve set up a password for the root account, or if you prefer not to use the root account at all and instead use a different username, you’ll need to fill in that information if you want your connection to work.

You can test to see if your connection is working by running the following command.

$ rake db:migrate

If you see nothing exceptional returned, congratulations! Rails can connect to your database. However, if you see something like this:

rake aborted!
Access denied for user 'root'@'localhost' (using password: NO)

then you need to adjust your connection settings. If you’re having problems, make sure that the database exists and that you’ve entered the correct username and password.

blog comments powered by Disqus

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials