If you don't like the command-line SQL interface, Lenz suggests the graphical MySQL Query Browser from MySQL AB that runs on Windows, Mac OS X or Linux. Another option is the open-source project phpMyAdmin.
Reminder: If you're doing a project with another name, it's important to use the Rails naming convention here (one of many examples of the Rails "convention over configuration" mantra). The root name of each database has to be the same name as whatever you've named your Rails project (the one I'm calling myfavorites
), and it's got to use the _development
, _test
and _production
endings. Otherwise, you start losing Rails' built-in associations, such as those in the database.yml
configuration file.
As Lenz describes it, Rails implements the MVC concept with several key components. ActiveRecord
, the model, "handle(s) all of an application's tasks that relate to the database." I no longer have to worry about setting up connections to my databases whenever I need to do a query. Instead, a Rails component handles that. Useful but not extraordinarily labor-saving, since most languages have pretty structured methods of setting up that initial database connection.
Much more interesting is the way Rails handles creation of tables that map to classes. "Rows map to individual objects, and the columns map to the attributes of those objects," says Lenz. Of course, I could go into MySQL directly and set up tables, but this bypasses some of the useful tools the Rails framework offers.
Since my little application will hold stories in a database, I need a model to match. I change to the myfavorites
directory (don't forget that part) and run the command
ruby script/generate model Story
You can create a model with the name of your choice, but it should be capitalized. My command, using the model name of Story
, creates several files, including story.rb
, which will store the class definition for the Story
model as well as other files that are set up for testing and adding data.
When issuing the command to create a model, some files already exist and others are newly set up. |
One of these autogenerated files is what's called a database migration file. This can add tables to my database and fields to my tables within the Rails framework. Each time I make changes to my database structure, I use another migration file. They're numbered, making it easy to roll back my changes, or decide later that I want to add, drop or alter fields.
Inside the db/migrate
folder, I open 001_create_stories.rb
in a text editor (not a word processor -- you don't want extra characters added to your code that can cause problems when running scripts). This file was also auto-generated when I created the Story
model.
Text editors for Ruby
Another option, ActiveState Software Inc.'s free Komodo Edit, handles Ruby syntax and is available for Linux, Mac OS X and Windows. UltraEdit ($50) from IDM Computer Solutions Inc. is another popular text editor that handles Ruby. There is also a free, open-source RoR development environment called Aptana RadRails that runs on Linux, Mac OS X and Windows. Lenz also suggests Eden Kirin's free ConTEXT on Windows and Allan Odgaard's TextMate (approx. $40) on Mac OS X. According to Lenz, TextMate is "the hands-down winner in the Rails code editor popularity contest." |
||||||
Rails project step by step
If you're working on your own project, with whatever text editor you've chosen, you can edit that 001_create_yourmodelname.rb
file to add a table to your database and fields to your table.
Hands on with Ruby on Rails
|
My 001_create_stories.rb
file comes prepopulated with this:
class CreateStories < ActiveRecord::Migration
def self.up
create_table :stories do |t|
# t.column :name, :string
end
end
def self.down
drop_table :stories
end
end
I add the bolded lines below to create fields in the table:
create_table :stories do |t|
t.column :contentid, :integer
t.column :headline, :string
t.column :summary, :text
end
They're now part of the new self.up
method. Those self.up
instructions are executed when the migrate command runs the file. (The automatically generated self.down
method deletes the table, which will only be executed if I want to roll back and undo the table creation.)