Monday, August 29, 2011

Rails 3.0: RSpec 2 + Machinist + ActiveRecord

RSpec 2 usually cleans up after itself if you set config.use_transactional_fixtures to true. However, if you use Machinist 2, this isn't the case. Apparently Machinist likes to keep around a cached version to speed things up. This is bad when trying to run independent tests.

To prevent this from happening, add this to config/environments/test.rb.
Machinist.configure do |config|
  config.cache_objects = false
end

References

http://blog.angelbob.com/posts/315-RSpec-not-clearing-database-when-you-use-machinist---published-rails

Sunday, August 28, 2011

Rails 3.0: PostgreSQL

If you're starting fresh, getting a Rails application running on PostgreSQL can be rather involved.

Install PostgreSQL on Ubuntu.
sudo apt-get install postgresql libpq-dev pgadmin3 -y

Create a new Rails application with PostgreSQL.
rails new <project name> -d postgresql

If this is an old project, install the gem in Gemfile.
gem 'pg'

Create the PostgreSQL user. The username will be the same name as your project which can be found in config/database.yml.
sudo su postgres -c psql
# Enters the psql console.
create user "<username>" with [superuser] password "<password>";
# Outputs CREATE ROLE.
\q
The superuser keyword is optional but makes things easier in development.

If you get the error "FATAL: ident authentication failed", edit /etc/postgresql/8.4/main/pg_hba.conf.
local all all ident
to
local all all md5

Restart PostgreSQL.
sudo service postgresql restart

Create the development and test databases through the terminal.
createdb <project name>_development -U <username> -W
createdb <project name>_test -U <username> -W

References

http://olmonrails.wordpress.com/2008/08/12/switching-rails-to-postgresql/