Ruby on Rails/Examples
This section is a collection of useful Rails examples.
important note
[edit | edit source]rails command now start with "rails" and not "script/"
Step By Step
[edit | edit source]How to Add a New Table
[edit | edit source]script/generate model <Name>
Generate the empty model and migration file.
vi db/migrate/XXX_create_<Name>.rb
Add columns to the table.
rake db:migrate
Migrates the data level - that is - creates the new database table.
vi app/models/<Name>.rb
Define the validations, sizes, etc.
vi test/unit/<Name>_test.rb
Define the unit tests that exercises the model validations.
If there will be a controller (and views) associated with this Model:
script/generate controller <Name> <action_one> <action_two> ...
Creates the controller and creates a view for each action.
Find
[edit | edit source]The find method of ActiveRecord is documented in the Rails API manual
pet = Pet.find(pet_id)
Find record by id (an integer).
Note: Returns one object. pet_id should be primary number.
pets = Pet.find(:first, :conditions => ["owner_id = ?", owner_id])
- returns the first matching record.
[Note: Returns one object.]
pets = Pet.find(:all, :conditions => ["owner_id = ?", owner_id])
- find all records with a given field value. [Notes: 1. Returns an array of objects. Check for no records found with: pets.empty?. 2.:conditions => supplies an SQL fragment used with WHERE *]
pets = Pet.find(:all, :conditions => ["owner_id = ? AND name = ?", owner_id, name])
- find all records matching multiple field values. [Note: OR also works.]
pets = Pet.find(:all, :conditions => ["name LIKE ?", "Fido%"])
- find all records matching a pattern. Wild cards are % for zero or more of any character, and _ for any single character. To escape a wild card use \% or \_. The reference from MySQL for LIKE will help. On the MySQL Regex website you will find examples for using REGEX.
pets = Pet.find(:all, :order => 'name')
- find everything and sort result by name.
pets = Pet.find(:all, :limit => 10, :conditions => ["owner_id = ?", owner_id])
- returns no more than the number of rows specified by :limit.
pets = Pet.find(:all, :offset => 50, :limit => 10)
- uses offset to skip the first 50 rows.
Rake
[edit | edit source]Migrations
[edit | edit source]$ rake db:migrate
- migrate to latest level by executing scripts in <app>/db/migrate.
Note: Migration scripts are created by script/generate model <mod-name>
Testing
[edit | edit source]$ rake
- run all tests.
$ rake test:functionals
- run the functional tests, which test the controllers.
$ rake test:units
- run the unit tests, which test the models.
$ test/functional/<name>_controller_test.rb
- run one functional test.
Documentation
[edit | edit source]$ rake doc:app
- generate Ruby Docs for the application. Docs are placed at <app>/doc/app/index.html.
Clean Up
[edit | edit source]$ rake log:clear
- delete all logs.
$ rake tmp:clear
- delete temporary files.
Server
[edit | edit source]$ script/server
- start the web server for this app. By default, the server is running in development mode. By default, it will be accessible at web address: http://localhost:3000/
$ RAILS_ENV=test script/server
- start the web server in Test Mode.
$ script/server -e test
- start the web server in Test Mode.
$ script/server -e production
- start the web server in Production Mode (more caching, etc.).
Fixing Errors
[edit | edit source]can't convert Fixnum to String
[edit | edit source]some_number.to_s
- every Fixnum has method .to_s to convert it to a String.
Shell Commands
[edit | edit source]Certain useful shell commands that I'm always trying to remember:
grep -r hello .
- run grep, searching for 'hello', on every file in the tree starting with the current directory.
tar -cvzf archive.tgz <targ_directory>
- tar up directory and compress it with gzip.