Establish a 2nd database connection in Rails

OK, you can find some howto’s everywhere on the web but only on this site I found the complete solution:

[sourcecode language=’ruby’]
class SecondConnection < ActiveRecord::Base SecondConnection.establish_connection( :adapter => “informix”,
:host => “localhost”,
:username => “hansnase”,
:password => “very_secret”,
:database => “ccdb@localhost”

self.abstract_class = true

“self.abstract_class = true” makes the thing actually work. Otherwise Rails seems to cache the database connection and does not switch back if you access any other classes but SecondConnection.


Later: Not so Yeah…

I had to establish another call to
[sourcecode language=’ruby’]
ActiveRecord::Base.establish_connection { “Establishing Base Connection” }

I assume it’s either an Informix problem or related to the old rails version I’m driving with.
Nevertheless: It works now.

Rails and incoming mails on a Mac Leopard workstation

Obviously I’m in a need of such an installation, otherwise I wouldn’t have googled about this (btw.: Do you know

Here are some snippets from:

  1. ChrizDee
  2. Craig Ambrose

And? What did I do:

I first enabled Postfix using 1). After this I have changed /etc/aliases:

[sourcecode language=’ruby’]

echo ‘rails_mailer: “|/usr/local/bin/mail_handler.rb”‘ >> /etc/aliases
chmod 755 /usr/local/bin/mail_handler.rb”‘ >> /etc/aliases
sudo newaliases
sudo postfix reload


[sourcecode language=’ruby’]
require ‘net/http’
require ‘uri’Net::HTTP.post_form URI.parse(‘http://localhost:3000/emails’), { “email” => }


The script above will be triggered by every mail that will be sent to rails_mailer@localhost (resp. your hostname). It will put a post-request to localhost:3000/email including the email in the parameter “email”.

Hpricot: XML to Hash

Well: I nearly lost all my hairs about this. Strange that I couldn’t find something like this in the docs.Situation:

I have some XML code from that I need parts being put into a simple hash (see below).

The following small ruby snippet does exactly this.

Only thing that’s not working is the “inner_xml” part. I personally don’t need it but your mileage may vary and you may want to fix the issue (and give me a note)

[sourcecode language=’ruby’]
require ‘rubygems’
require ‘hpricot’
require ‘pp’

text = < 0

Franz Fluchsfinger


Just some data
inner text


xml =Hpricot.XML(text)
a= xml/”creditcard/*”
a.each {|b|
h[] = b.inner_text if b.is_a? Hpricot::Elem
pp h



ruby test.rb
"xml_for_demo"=&gt;"inner text",
"holder"=&gt;"Franz Fluchsfinger",
"data"=&gt;"Just some data"}

Btw.: I found the solution partly on

Rails Snippets — Arbitrary SQL

Seems I’m in a Ruby/Rails mood today

Sometimes you don’t need the whole Rails stuff and just want to place a query.

Try the following:


will do the trick.

list = ActiveRecord::Base.connection.select_all("select a,b from c_table")


[ {a =&gt; Value_for_a, b =&gt; Value_for_b}, {a =&gt; Value...}...]

number_of_rows = ActiveRecord::Base.connection.update("update month set month_id = month_id +1 where month_id = 11")

If you make use of an already created Rails Model instead of the base class, you will get the benefit of accessors (instead of hash-keys) for the  selected objects.

Example (necessary, I know 🙂 )

  • create the model

# script/generate model CTable

  • use it in your code

list = CTable.find_by_sql("select a,b from c_table")

  • access elements

pp list[0].a

Rails Snippets

OK, this is more Ruby than Rails but nevertheless helpful…

If you come from a Perl background you will be surprised that in Ruby an array is definitively not a list. So if you want to pass a list of elements to a method that you have stored in an array, you may want to use the following idiom:

list = []
lines = File.readlines("FILENAME")
lines.each do |line|
list.push(*line.split(" ")))

The *line.split(" ") creates an element list out of the array that the split command has created.

I fear a real Rubyist would have done it in a different way but OK.

P.S. Does someone know what I’m doing wrong with the code formatting in WordPress? I’m quite sure to have included whitespaces in the code above…

Rails Snippets

Some small snippets of Rails/Ruby codes that I’d like to not forget…

I found an interesting site on the Web: 

Tweaking it a bit:

require '../config/environment'
require 'pp'
id = ARGV[0]
if id.nil?
raise &lt;<eof>
app =
app.get "/kunden/status_mail/#{id}"</eof>

This works fine from a Rails subdirectory and calls the same action like http://RAILSSITE/kunden/status_mail/ID