The blog has moved to http://jessehouse.com/ ... Many google searches point here so I am leaving it operational, but there will be no new posts.

Friday, August 20, 2010

ruby 1.9.2 and rails 3rc with rvm

Get up and running with rails 3 release candidate on ruby 1.9.2

First step install rvm (ruby version manager) - note that rvm does not work on windows, for windows try pik

You will need to wait between each step as installation times will vary

# install 1.9.2 using rvm
 rvm install 1.9.2  
# this installed the official release 1.9.2-p0

# switch to 1.9.2
 rvm 1.9.2 
# gem install rails rc
 gem install rails --pre 

# create a new rails app, in this case 'sample-r3', this differs from older versions of rails; new is a required arg 
 rails new sample-r3 
# navigate to the root of your new rails app
 cd sample-r3 
# install the app dependencies - in this case sqlite3, see Gemfile in the root dir
 bundle install 

# generate a user scaffold, note g is shortcut for generate
 rails g scaffold User name:string email:string 
# create the database table users
 rake db:migrate 
# start up the rails server, note s is shortcut for server
 rails s 

Browse to http://localhost:3000/users and we are up and running and yes the scaffold pages still have the same old look and feel






In the past I have tried installing and running rails3 beta and rc on different versions of ruby and ran into some serious difficulties; but it has been painless on the official release of ruby 1.9.2

As a final step we want to add a .rvmrc file to the root of the rails project, this will force rvm to load the correct ruby version when we are in this directory. This can be quite handy, if we were on a different version of ruby running 'rails s' would create a new rails app directory named 's' instead of running the server; from the terminal run

 echo "rvm 1.9.2" > .rvmrc 

I learned about the .rvmrc file from this post

Thursday, August 19, 2010

Updated Look!

Just switched to a different Blogger Template - much wider and mo better!

Also consolidated my posting, I had 20+ code samples on another blog. Used the Blogger import / export tool which was pretty seamless; needed to move a few code blocks over to gists to get proper formatting but that was it!

Wednesday, August 18, 2010

valid? vs errors.any? in rails

Recently got bit by checking valid? in some rails code, thought I would share.

In this situation I was manually adding to the errors collection of an ActiveRecord object and then calling valid? to see if the object was ok; this is not the normal style of validation on ActiveRecord objects, if I had been playing by the rules valid? would have done what I needed; it would check the current state of my objects attributes and 're-validate' the object.

This screen shot of an irb console session shows the issue I was running into


valid-v-errors-any

Taking a look at the source code for valid? it becomes obvious why the manually added errors get cleared, just not what I expected to happen


valid-source