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
end
[/sourcecode]

“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.

Yeah!

Later: Not so Yeah…

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

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

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


master
Franz Fluchsfinger

52660xxxxxxxxxxx79

09/10
Just some data
inner text
a5f4e2shortened4b1e523ca873837c8f1c9ea66ee1924d

eof

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

[/sourcecode]

result:


ruby test.rb
{"company"=&gt;"",
"xml_for_demo"=&gt;"inner text",
"no"=&gt;"52660xxxxxxxxxxx79",
"holder"=&gt;"Franz Fluchsfinger",
"signature"=&gt;"a5f4e2shortened4b1e523ca873837c8f1c9ea66ee1924d",
"type"=&gt;"master",
"valid_until"=&gt;"09/10",
"kpn"=&gt;"",
"data"=&gt;"Just some data"}

Btw.: I found the solution partly on http://railsforum.com/viewtopic.php?id=22055

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(Result.new(*line.split(" ")))
end

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: http://matthall.wordpress.com/2006/12/06/how-to-execute-a-rails-controller-action-via-cron/ 

Tweaking it a bit:

require '../config/environment'
require 'pp'
id = ARGV[0]
if id.nil?
raise &lt;<eof>
USAGE: $0 RESELLER_ID
eof
end
app = ActionController::Integration::Session.new
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