The Rails framework is an efficient tool for producing web applications using the Ruby language. Here are steps to create a database backed web application. Here are the steps to create a sample application that provides access to a sample UPC database of grocery items. The database was provided as an spreadsheet. Each row of the spreadsheet corresponds to an item and each item has over 80 properties. In this sample, I’ll create a rails application that allows the user to view, find and edit the grocery database.
- Create the application folder:
$rails upcand then change to this directory. - Modify the file
./config/database.ymland add the username/password for your development, test and production databases. - Generate the model. Here we only have one table (a list of grocery items). The class Item corresponds to one row of this database.
$ruby ./script/generate model ItemUse Rails’s schema migration to create the database. Schema migration is a database independent way of creating a database schema. By default Rails (1.1 and above) uses Active Record instead of SQL scripts. - Configure the database by editing the file
./db/migrate/001_create_items.rb
and modify theupmethod to include all of the columns of your database table, e.g. look like this:
class CreateItems < ActiveRecord::Migration
def self.up
create_table :items do |t|
# add all columns of your database table, here is a sample
t.column "status", :string
t.column "date", :date
t.column "upc-e", :string
t.column "ucc-12", :string
end
end
def self.down
drop_table :items
end
end - Run the
rakecommand to create (& upgrade?) the database tables (you’ll need to create the database (production, development, and test databases) before hand, e.g., log-in to MySQL and createupc_developmentdatabase before you run therakecommand):
rake migrate - Now that the database has been setup, its time to initialize the database. You can do this by creating a fixture. It helps if you already have your data in the form of a CSV file. Simply copy one CSV file for each database table (if the name of the table is
items, then the name the CSV fileitems.csv). Copy the CSV file to the./test/fixturesfolder. Use therakecommand to load the CSV file:
rake db:fixtures:load
If the columns in the CSV are blank, then loading in MySQL will fail with error such as Data truncated for a specific column. You can edit the CSV file and replace these blanks with suitable initial values (there must be an easier way) - Once the database has been initialized, you can use the rails console application to dynamically explore the mode (view and edit). Here is how you can start the console app:
This will load the database in sandbox mode, assign variable
$ruby script/console -s
Loading development environment in sandbox
Any modifications you make will be rolled back on exit.
>> a = Item.find(3)
=> ...
>> y a
=> ...
ato the 3rd row of the table and then list all the columns of this row. - Create the Controllers…. TBD
- Create the Views…TBD
- Start the server, and visit the application….TBD
Advertisement