Using HTMLEntities 06 Oct 2010
Tags: gem

I recently needed to decode/encode a string which will be used as text on one of the sites I'm syndicating to.

The problem:

The site escapes all html entities and tags. And since I'm posting to the site using Mechanize, I need to format the text so that it would come out clean and nice.

The solution:

HTMLEntities. I never really bothered what the HTMLEntities gem was. Never even wondered why it was a dependency of some gems/plugins I regularly use. Now I know. :D

Installing ruby in ubuntu with RVM 11 Oct 2010
Tags: gem, ubuntu, rvm

I needed to use different ruby versions in one box so I looked for a good way to manage ruby versions. One google search and I've found what I'm looking for, RVM. So the first stop is Railscasts. Episode 200 shows how to install Rails 3 using RVM.

After some experimentation with the help of the screencast and the rvm documentation, I was able to make everything working after encountering errors with zlib and readlines. So as a general rule of thumb, install all available package that comes with rvm. Use the following command

ruby
rvm package install ree_dependencies

to install zlib, ncurses, readline, openssl and iconv. Then you can install any ruby version with the following command

ruby
rvm install <ruby version> -C --with-readline-dir=$rvm_path/usr,--with-iconv-dir=$rvm_path/usr,--with-zlib-dir=$rvm_path/usr,--with-openssl-dir=$rvm_path/usr

By the way, I'm using Ubuntu 10.10.

Setting primary key in Rails 2.2.2 26 Oct 2010
Tags: primary_key, rails 222

This is a super late discovery. I've been refactoring some code of a previous developer in a Rails 2.2.2 project. I needed to eager load some associations as well to avoid n+1 queries but I don't know how to do that if the primary key is not ID. After searching through google, I found a post somewhere (sorry, can't remember the link. But I'm sure it's in stackoverflow). All you need to do is set the primary key for that model by adding

ruby
Class Foo > ActiveRecord::Base
  set_primary_key :pkey

  has_many :bars, :foreign_key => :fkey
end

Then you can use the normal belongs_to

ruby
Class Bar > ActiveRecord::Base
  belongs_to :foo, :foreign_key => :fkey, :class_name => 'Foo'
end