Cheat Sheet

Ruby on Rails For Dummies

From Ruby on Rails For Dummies by Barry Burd

Ruby on Rails is an open source framework you can use to build Web sites and Web-based databases. Of course, as with any programming language, you need to know Ruby’s keywords and Rail’s naming conventions. Making sure that your data meets validation standards is key, and the proper iterators make traveling amongst your data a breeze.

Ruby on Rails Keywords

If you’re using Ruby on Rails to create your Web site or database, you need to know the keywords Ruby uses. Fortunately, you have access to the following table, which lists Ruby’s keywords:

alias defined? __FILE__ not then
and do for or true
BEGIN else if redo undef
begin elsif in rescue unless
break END __LINE__ retry until
case end module return when
class ensure next self while
def false nil super yield

Naming Conventions for Ruby on Rails

You’re using Ruby on Rails to create a Web application or Web database app, which is very smart of you. Depending on what you’re working with — an application, a one-to-many relationship, or a many-to-many relationship — you use different variations on Rails naming protocols, which are explained in the following sections.

Ruby naming for new apps

When you create a new application — for example, an album project with a photos database table — use the following steps:

  1. Create a Rails project named album.

  2. Create databases named album_development, album_test, album_production.

  3. Generate a Photo model. (In the RadRails Generators view, select model in the drop-down list, and type Photo in the text field to the right of the drop-down list.)

    • Rails creates a class named Photo in a file named photo.rb.

    • Rails creates a migration file named 001_create_photos.rb.

  4. Create a database table named photos.

  5. Generate a Photo scaffold. (In the RadRails Generators view, select scaffold in the drop-down list and type Photo in the text field to the right of the drop-down list.)

    • Rails creates a class named PhotosController in a file named photos_controller.rb.

  6. Visit http://localhost:300x/photos/.

Ruby naming in a one-to-many relationship

When you work with a foreign key in a one-to-many relationship (for example, one photo with many comments), follow these tips:

  • The comments table has a photo_id column.

  • The Comment model contains the statement belongs_to :photo.

  • The Photo model contains the statement has_many :comments.

Ruby naming in a many-to-many relationship

When you work with a many-to-many relationship (for example, photos and tags), keep these protocols in mind:

  • The Photo model contains the statement has_and_belongs_to_many :tags.

  • The Tag model contains the statement has_and_belongs_to_many :photos.

  • The photos_tags table (so named because photos comes before tags alphabetically) has no id column.

Ruby on Rails Validation Helpers

When you create a Web site or Web application with Ruby on Rails, you need to make sure that you input data in a form that Rails recognizes and can use. The following table contains Rails validation helpers:

validates_acceptance_of validates_inclusion_of
validates_associated validates_length_of
validates_confirmation_of validates_numericality_of
validates_each validates_presence_of
validates_exclusion_of validates_size_of
validates_format_of validates_uniqueness_of

Useful Iterators and Methods for Ruby on Rails

When you want to travel through the items in a database you created with Ruby on Rails, knowing the iterators to use is key. The following table shows helpful iterators and methods:

[1, 2, 3].each { } => [1, 2, 3]
[1, nil, nil, 2, 3, nil].compact { } => [1, 2, 3]
[1, 2, 3].delete_if { |x| x >= 3 } => [1, 2]
[1, 2, 3].collect { |x| x + 1 } => [2, 3, 4]
[1, 2, 3].find_all { |x| x % 2 == 1 } => [1, 3]
[1, 2, 3].reject { |x| x % 2 == 1 } => [2]
[2, 5, 1, 0, 7].sort => [0, 1, 2, 5, 7]
[2, 5, 1, 0, 7].max => 7
[1, [2, 3]].flatten => [1, 2, 3]
[1, 2, 3].empty? => false
[].empty? => true
[0, 5, 9].length => 3
[1, 2, 3].include?(2) => true
[1, 2, 3].include?(16) => false
[1, 2, 3].reverse => [3, 2, 1]
blog comments powered by Disqus
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win $500. Easy.