You want your web application to store persistent data in a relational database.
The hardest part is setting things up: creating your database and hooking Rails up to it. Once that's done, database access is as simple as writing Ruby code.
To tell Rails how to access your database, open your
file. Assuming your Rails application is called
mywebapp, it should look something like
development: adapter: mysql database: mywebapp_development host: localhost username: root password: test: adapter: mysql database: mywebapp_test host: localhost username: root password: production: adapter: mysql database: mywebapp host: localhost username: root password:
For now, just make sure the
development section contains a valid
username and password, and that it mentions the correct adapter name
for your type of database (see Chapter
13 for the list).
Now create a database table. As with so much else, Rails does a lot of the database work automatically if you follow its conventions. You can override the conventions if necessary, but for now it's easiest to go along with them.
The name of the table must be a pluralized noun: for instance, "people", "tasks", "items".
The table must contain an auto-incrementing primary key field
For this example, use a database tool or a
CREATE DATABASE SQL command to create a
mywebapp_development database (see
the chapter introduction for Chapter 13 ...