Freelancing Gods 2014

09 Mar 2008

Migrating Code from Rails to Merb

Here’s a collection of notes made while I was working on migrating this blog from Rails to Merb (no, the Merb version isn’t live yet). These are relevant to version 0.5.2 – but Merb’s moving so fast these days, I wouldn’t be surprised if much of this isn’t relevant any more.

Most of this you can find if you look through the documentation, but if you’ve not yet played with Merb, this will hopefully give you some idea of some of the smaller differences.


  • Use before and after instead of before_filter and after_filter
  • Instead of :except, use :exclude.
  • To kill the chain of filters and not follow on to the action method, you need to use the throw method:
def confirm_user_session
  throw :halt, { |controller|
    controller.redirect url(:new_session)
  } if current_user.nil?

Oh, and in case you missed it in the above sample, redirect_to becomes redirect.


Resource routing isn’t actually any different to Rails – but I had trouble finding any documentation for it, so it wasn’t obvious to start with. This code will work in both Merb and Rails (although in Rails r is usually referenced as map)

r.resources :posts do |post|
  post.resources :comments

However, using these routes is definitely different. The url_for, route_url and route_path methods aren’t around – you need to use Merb’s magical url method:

url(:post, @post@)

View Helpers

Helpers are pretty minimal in Merb – you don’t get any of the inflectors, or the number formatters. And no form helpers – not in the core gem, anyway. You can get some of those from the merb_helpers gem, but they don’t match Rails’ syntax and method naming, so it can take a bit of time to get this switched over, depending on the size of your app.

No support for Form Builders in that gem, by the way.

Some of the default helpers that do exist have different names to their Rails counterparts. A few of the ones I came across:

  • content_tag => tag
  • tag => open_tag
  • content_for => throw_content
  • javascript_include_tag => js_include_tag
  • stylesheet_include_tag => css_include_tag

All your favourite plugins

Because Merb plugins are gems, anything you use as a plugin in Rails is pretty unlikely to be ported over. will_paginate was the main one for me, so I ended up pulling the files into my lib directory. Of course, that was only really useful when using ActiveRecord – DataMapper and Sequel users may have to get hacking into any ActiveRecord-focused plugins they want to use.


No more render :partial – you want something more like the following:

partial "comments/show", :with =>
partial "post", :with => @posts, :as => :post

Again – you’ll find most/all of this in the documentation, but the only potential show-stopper I found was plugins – the rest isn’t that that big a difference to Rails.

23 Feb 2008

Talking to MYOB with Ruby

Okay, time for another code post. Prompted by a comment on an earlier post about Merb and MYOB, I thought I’d provide some more detail on how to talk to MYOB using Ruby, for any other people stuck in a similar position.

ODBC Bindings

If you like crafting your own SQL, this is the easiest approach. Firstly, you’ll need to download Christian Werner’s ODBC Bindings for Ruby, and install it using the following commands (or something along these lines) from within the source directory:

ruby extconf.rb
sudo make install

Documentation is a bit light on examples – and it’s really just a hook into the C libraries for ODBC, so good luck reading the source. Here’s some of the basic things you’ll need to do. (Don’t forget to require 'odbc' first, of course).

Setting up a Connection

You can either go through the DSN collection, or connect using the string name of the DSN (which in this case we’ll assume is ‘MYOB’):

dsn = ODBC.datasources.detect { |source| == "MYOB" }
database = ODBC::Database.connect dsn

# or

database = ODBC::Database.connect "MYOB"

Note that the ODBC.datasources collection only contains system DSNs, not User-level ones. Not sure why, or how to access the latter.

SELECT Statements

statement = database.prepare "SELECT * FROM Customers"
statement.each_hash d |row|
  # results accessed as row["column_name"]

Make sure you drop the statement once you’re done with it, otherwise you’ll get complaints when you stop ruby that the statements weren’t all closed and dropped.

There’s other ways to access results – you can go through results as you see fit using fetch or fetch_hash, depending on whether you want an array or hash of the record.

INSERT Statements

With other SQL statements (although there’s no use of UPDATE or DEETE in MYOB connections), you don’t really care about the results, so that makes things a bit simpler, you can just call"INSERT INTO Import_Customer_Cards (...) VALUES (...)"). The do method creates the statement, executes it and then drops it for you. MYOB makes this a little more complex though, as it all must be done in transactions – not that that’s a bad thing.

database.transaction do |env|"INSERT INTO Import_Customer_Cards (...) VALUES (...)")

This won’t actually do anything though – and it won’t throw up an error or warning. The problem is you need to turn autocommit mode off, because the MYOB ODBC drivers don’t like it (it’s not a problem with Christian’s bindings). So, back when you create your connection:

database = ODBC::Database.connect "MYOB"
database.autocommit = false


If you prefer some level of abstraction above the messy SQL, then it might be worth looking at OpenLink’s ActiveRecord ODBCAdapter. I recommend the gem instead of the plugin, as the plugin takes it upon itself to modify your code, which I don’t like.

sudo gem install odbc-rails

Now, I’m not an ODBC expert, but there seems to be different flavours of ODBC connections possible – and MYOB’s is not one supported by this gem. So, if you’re using Rails, put the gem into vendor (and for Merb, into the local gems folder), then modify it with this patch. I make no promises for it being stable or reliable – but I’ve not had any problems yet.

Of course, using ActiveRecord is viable if you’re just reading data out – but if you want to write as well as read, then there’s issues. MYOB has separate write tables (prefixed with ‘Import_’), and they have denormalised schemas compared to the read-equivalents.

For database.yml, you’ll need something like the following:

  adapter: odbc
  dsn: MYOB

Everything Else

In both the situations above, I’ve not put usernames or passwords into the connections – you can, but that is already handled by the ODBC DSN, so I keep my code that bit simpler.

For those who are new to coding for MYOB – it does cost money to get a developer account (several hundred dollars per year), which is the only way to get write access. I think read access is a once off fee of a couple of hundred dollars, but that’s specific to a MYOB file.

And if anyone is wondering – while I was initially using the odbc-rails library, I’ve now switched to constructing the SQL myself and just using the bindings, because of the table issues.

21 Jan 2008

Mixing Merb and MYOB

For one of the contracts I’m working on at the moment, I’ve been using Merb to construct a web service that interacts with MYOB, and can be consumed with ActiveResource.

The connection to MYOB is ugly, using Christian Werner’s ODBC Bindings and the Rails/ActiveRecord ODBC Adapter, the latter of which had to be hacked slightly. However, the Merb side of things was quite clean. I’m really looking forward to seeing how Merb progresses, especially with their plans for merb_core and merb_more.

One of the rare snippets of code from the Merb app that I think is more verbose than the Rails equivalent is how to go about obtaining the query parameters (as opposed to routing parameters) of a request.

The Rails way:


The Merb way:

params.except *request.route_params.keys.collect { |key| key.to_s }

Also, in case you’re as stupid as I am and want to generate Merb controllers on the fly, you can’t use The only way is by building the class in a string and eval’ing it:

Object.send(:eval, <<-CODE
class ::Object::#{controller_name} < Application
  # actions and such go here

It’s not particularly elegant, but at least it works.

30 Dec 2007

Postcode API

A couple of weeks ago I quickly coded a basic webservice – using Merb – for Australian Postcode data. Just got hosting for it sorted out last night, so now I can blog about it.


The emphasis is on simple – you can search by postcode or suburb, and get the data back either as a HTML page, XML, or JSON. That’s pretty much it. The request urls aren’t complicated, either:

Suburb requests use partial matching, so you don’t need the full suburb name. If you want the JSON returned with the MIME type of application/json, use the .json extension.

I’ve no plans at this point on using this, but perhaps it’s useful for someone out there – if so, would love to hear about it.

RssSubscribe to the RSS feed

About Freelancing Gods

Freelancing Gods is written by , who works on the web as a web developer in Melbourne, Australia, specialising in Ruby on Rails.

In case you're wondering what the likely content here will be about (besides code), keep in mind that Pat is passionate about the internet, music, politics, comedy, bringing people together, and making a difference. And pancakes.

His ego isn't as bad as you may think. Honest.

Here's more than you ever wanted to know.

Ruby on Rails Projects

Other Sites

Creative Commons Logo All original content on this site is available through a Creative Commons by-nc-sa licence.