Most people are fairly familiar with the basic idea of a database driven website. More specifically, they understand that data is stored in the database and somehow makes it into the web page the browser loads.
Ruby on Rails provides a framework that makes the transition of information from a database to a webpage extremely simple. To begin, we’ll assume that you have Ruby and Rails installed on your machine. From there, we’ll work our way up from the database to the core of the application and back out into the webpage that presents the data.
First, create your new application
In this example, we’ll be creating a simple blog.
rails simple_blog
Second, set up your database
In this case, steps 2 and three blur a little. This is because Ruby on Rails provides scripts called generators, which create certain files with certain functionality built in. We’ll explain that part in a minute.
First, we need to tell your new blog where the database is that it will be using. To do that, open the file at config/database.yml in your favorite text editor. In this tutorial, we’ll be using vim.
cd simple_blog vim config/database.yml
You should see a file that looks something like this:
# SQLite version 3.x # gem install sqlite3-ruby (not necessary on OS X Leopard) development: adapter: sqlite3 database: db/development.sqlite3 pool: 5 timeout: 5000 # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: adapter: sqlite3 database: db/test.sqlite3 pool: 5 timeout: 5000 production: adapter: sqlite3 database: db/production.sqlite3 pool: 5 timeout: 5000
We are going to change our database configuration to use mysql.
development: adapter: mysql database: blog_development username: root timeout: 5000 # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: adapter: mysql database: blog_test username: root timeout: 5000 production: adapter: mysql database: blog_production username: root timeout: 5000
In this example, the root password is blank. You can add a ‘password:’ field to each database that needs a password where necessary. The ‘pool:’ field we left out will cause Ruby on Rails to connect across a pool of connections. The default in this case is 5 connection per pool.
Now that we’ve told Ruby on Rails where to go for its data, it’s time to begin defining that data. The types of data we’ll be dealing with in these cases are: users, posts, and categories. We can define these data types by using the generator scripts:
script/generate model user username:string password:string full_name:string script/generate model post title:string content:text script/generate model category name:string
You’ll see the generator creates several files. The one we’re going to talk about for step 2 is the database migration. The database migrations are scripts that modify your database. Let’s open the database migration for our user data.
vim db/migrate/*_create_users.rb
class CreateUsers < ActiveRecord::Migration def self.up create_table :users do |t| t.string :username t.string :password t.string :full_name t.timestamps end end def self.down drop_table :users end end
The migration above does exactly what it looks like it does. It creates a table with three string columns and some timestamps. It also creates a column on each table called ‘id’ that is the unique id for each row in the table.
Now that we have the database migrations ready to go, let’s run them and get our database ready to start accepting data.
rake db:migrate
Now that you’re set up and have run your migrations, open up your MySQL browsing software or command line and check out your database structure and we’ll come back around to Models accessing your data in Part II.