Prev | Current Page 87 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"

establish_connection(
:adapter => "mysql",
:host => "localhost",
:username => "project",
:database => "project_development")
class Role < ActiveRecord::Base
end
class Account < ActiveRecord::Base
belongs_to :role
end
admin_role = Role.find_or_create_by_name("Administrator")
account = Account.new
account.username = "cpytel"
account.role = admin_role
account.save
CHAPTER 1 ?–  INTRODUCING ACTIVE RECORD 23
puts "#{account.username} (#{account.id}) is a(n) #{account.role.name}"
# cpytel (1) is a(n) Administrator
# comment out the following line to avoid deleting the created account
account.destroy
puts "We have just deleted the #{account.username} account!"
Them??™s the Basics!
Believe it or not, in just one chapter, we??™ve introduced you to Active Record and walked you
completely through installing and configuration; plus, we??™ve built and explained complete
working programs showing the basic CRUD operations. It really is amazing how little you need
to do to get started with Active Record!
Of course, there??™s a lot more to Active Record than just the basics we??™ve covered here
(otherwise, this would be a very short book!).


Pages:
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99