Contents 

Ruby on Rails: Up and Running
Table of Contents
Copyright
Preface
Chapter 1. Zero to Sixty: Introducing Rails
Section 1.1. Rails Strengths
Section 1.2. Putting Rails into Action
Section 1.3. Organization
Section 1.4. The Web Server
Section 1.5. Creating a Controller
Section 1.6. Building a View
Section 1.7. Tying the Controller to the View
Section 1.8. Under the Hood
Section 1.9. What's Next?
Chapter 2. Active Record Basics
Section 2.1. Active Record Basics
Section 2.2. Introducing Photo Share
Section 2.3. Schema Migrations
Section 2.4. Basic Active Record Classes
Section 2.5. Attributes
Section 2.6. Complex Classes
Section 2.7. Behavior
Section 2.8. Moving Forward
Chapter 3. Active Record Relationships
Section 3.1. belongs_to
Section 3.2. has_many
Section 3.3. has_one
Section 3.4. What You Haven't Seen
Section 3.5. Looking Ahead
Chapter 4. Scaffolding
Section 4.1. Using the Scaffold Method
Section 4.2. Replacing Scaffolding
Section 4.3. Generating Scaffolding Code
Section 4.4. Moving Forward
Chapter 5. Extending Views
Section 5.1. The Big Picture
Section 5.2. Seeing Real Photos
Section 5.3. View Templates
Section 5.4. Setting the Default Root
Section 5.5. Stylesheets
Section 5.6. Hierarchical Categories
Section 5.7. Styling the Slideshows
Chapter 6. Ajax
Section 6.1. How Rails Implements Ajax
Section 6.2. Playing a Slideshow
Section 6.3. Using Drag-and-Drop to Reorder Slides
Section 6.4. Drag and Drop Everything (Almost Everything)
Section 6.5. Filtering by Category
Chapter 7. Testing
Section 7.1. Background
Section 7.2. Ruby's Test::Unit
Section 7.3. Testing in Rails
Section 7.4. Wrapping Up
Appendix A. Installing Rails
Section 1.1. Windows
Section 2.1. OS X
Section 3.1. Linux
Appendix B. Quick Reference
Section 5.1. General
Section 5.2. Testing
Section 5.3. RJS (Ruby JavaScript)
Section 5.4. Active Record
Section 5.5. Controllers
Section 5.6. Views
Section 5.7. Ajax
Section 5.8. Configuring Your Application
About the Authors
Colophon
Index
A
B
C
D
E
F
G
H
I
J
L
M
N
O
P
R
S
T
U
V
W
X
Y
Z

Ruby on Rails for all.

Prev Page Next Page
Previous Page
Next Page

2.7. Behavior

Active Record lets you manipulate and find table data directly through Active Record classes and instances. If you want to work with data from a table, use the class. If you want to work with a table row, use an instance. ActiveRecord::Base class supplies many of the methods, and missing_method provides most of the rest. You can find the documentation for the latest stable Active Record version online at the following address: http://api.rubyonrails.com.

2.7.1. Finders

You can use other finders as well. find_by_sql lets you type SQL directly into a finder; find_all returns all records. In addition, Active Record adds a custom finder called find_by_<column_name> to each model class for each column in that model's table.

Let's use some of the methods on Photo. We'll use a finder and the destroy method to delete an object from the database:

Photo.find_by_filename("balboa_park.jpg").destroy

The methods delete and destroy are slightly different. delete aborts on minor errors, but destroy does not abort unless there's a critical database error. You can also update objects. Let's update the Photo object for cat.jpg:

>> cat=Photo.find_by_filename "cat.jpg"
...
>> cat.filename="Cat.jpg"
...
>> cat.update
...
>> puts cat.reload.filename
Cat.jpg
...

In this case, we found the cat.jpg record. We next updated the filename attribute and called update to write the changes to the database.

2.7.2. Validation

So far, you've used Active Record to do database operations on an object. You can also use Active Record for simple validation. For example, you can verify that the filename property exists with one line of code. Change the Photo class in app/models/photo.rb to look like this:

class Photo < ActiveRecord::Base
  validates_presence_of :filename
end

Let's see how the validation works. Go back to the console (you'll need to restart it to reload your changes), and try to save a blank photo:

>> photo=Photo.new
=> #<Photo:0x3501b70 @attributes={"filename"=>""}, @new_record=true>
>> photo.save
=> false

The save failed. Let's find out why:

>> photo.errors.each {|attribute, error| puts attribute + ": " +error}
filename: can't be blank
=> {"filename"=>["can't be blank"]}

You can do several different kinds of validation, or you can create your own. You could validate an email message like this:

validates_format_of :email,
                    :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i

Or you could validate the length of a field like this:

validates_length_of :name, :within => 6..100

Later, you'll see that the Rails view integration uses this information to present meaningful error messages; look for those details in Chapter 5. You've seen the basics of working with Active Record classes. You use a model object or its class to directly manipulate rows in the database table. Active Record goes beyond most traditional wrapping frameworks because it helps you manage relationships between tables. In the next few sections, let's look into how Active Record manages simple relationships.

2.7.3. Transactions

Photo Share doesn't require transactions, but for many applications, transactional behavior is critical. If you have some code that must be executed as a single unit, you can use Active Record transactions. The most common example is a transfer between two accounts. A transfer is fundamentally a debit and a credit. The Ruby code for a transfer between from and to Active Record Account models might look like this:

def transfer(from, to, amount)
  from.debit(amount)
  to.credit(amount)
end

You wouldn't want this method to fail after the debitif it did, the holder of the from account would be shorted by amount. So you use a transaction. This is the way it works:

def transfer(from, to, amount)
  Account.transaction do
    from.debit(amount)
    to.credit(amount)
  end
end

transaction is a method on all Active Record classes. With this approach, you can maintain the integrity of your transactions.


Previous Page
Next Page