Prev | Current Page 76 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"


Your First Example
Below is the source code for your first Ruby program that uses the Active Record library. The
program simply establishes a connection, creates an account object, and stores the attributes
of that account object in the database as a new record:
require "rubygems"
require_gem "activerecord"
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:username => "project",
:database => "project_development")
class Account < ActiveRecord::Base
end
account = Account.new
account.username = "cpytel"
account.save
CHAPTER 1 ?–  INTRODUCING ACTIVE RECORD 18
This simple Active Record program includes the Active Record gem, which you installed
previously. It establishes a connection to the project_development database with username
project.
Next, the Account class is defined. Notice that there is nothing in the class. Our Active Record
objects will eventually have stuff in them, but for now, its important to note that no configuration
is needed to get up and running with basic functionality.


Pages:
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88