I've been reading Agile Web Development with Rails, version 2005-9-13. The Ruby on Rails framework has been updated since this release. This page lists some of the errata that I came across and describes what changes to make so that you can follow the code examples.
A full comprehensive list can be found on the Pragmatic Programmer's web site: Errata for Agile Web Development with Rails - First Edition
Change MySQL tables from MyISAM to InnoDB
The SQL in the book is for MyISAM tables. The default config for the (Ruby on) Rails test framework sets use_transactional_fixtures = true in the test/test_helper.rb file, which means that transactional support is needed in order for the test fixtures to work. You could presumably change the variable to false, or you could change your database to one that supports transactions. For MySQL, this means you need InnoDB tables.
To convert the MyISAM tables to InnoDB, you have to
- Edit the SQL so that the foreign keys are indexed
- Make sure that any tables containing a referenced column are created before the tables which defines the foreign keys
So, the InnoDB SQL I used was as follows:
drop table if exists products;
create table products (
id int not null auto_increment,
title varchar(100) not null,
description text not null,
image_url varchar(100) not null,
price decimal(10,2) not null,
date_available datetime not null,
primary key (id)
)type=innodb;
drop table if exists orders;
create table orders (
id int not null auto_increment,
name varchar(100) not null,
email varchar(255) not null,
address text not null,
pay_type char(10) not null,
shipped_at datetime null,
primary key (id)
)type=innodb;
drop table if exists line_items;
create table line_items (
id int not null auto_increment,
product_id int not null,
order_id int not null,
quantity int not null default 0,
unit_price decimal(10,2) not null,
index (product_id),
index (order_id),
constraint fk_items_product foreign key (product_id) references products(id),
constraint fk_items_order foreign key (order_id) references orders(id),
primary key (id)
)type=innodb;
drop table if exists users;
create table users (
id int not null auto_increment,
name varchar(100) not null,
hashed_password char(40) null,
primary key (id)
)type=innodb;
For more details and the other requirements of InnoDB tables, see the 14.2.7.4. FOREIGN KEY Constraints page the MySQL 3.23, 4.0, 4.1 Reference Manual.
Fix for nil object error in Rails test fixtures
The following was copied from Fix for nil object error in Rails test fixtures from the CodeSnippets repository.
If you're seeing errors like this when you run Rails tests:
You might need to edit test/test_helper.rb to make sure use_instantiated_fixtures is true:
self.use_instantiated_fixtures = true
Prior to 1.0, Rails automatically created instance variables out of fixtures. So if you had a fixture record named "foo", you could access it in your test as "@foo". As of 1.0, the default is to disable that feature, which breaks a lot of existing code. Mike Clark
explains the change.