Playing with Merb
So there has been quite a lot of talk of late about merb, the new up and comer vying for the rails crown. It was initially developed by Ezra of EngineYard fame, and it's generally considered that 'They know their stuff'(tm). So what is merb? Is it a viable alternative to rails? How easy/hard is the migration from one to the other?
I figured it was long overdue that I take a look.
<h2>What is Merb? How is it different to Ruby on Rails?</h2>
Merb is a Rails-inspired MVC framework for web application development. It's major point of difference with Rails is that it's not so prescriptive on what external libraries you have to use. Don't like prototype/scriptaculous? Swap it out for JQuery or Moo. Don't like ActiveRecord? Try DataMapper or Sequel instead. One of the implications of this is that it means that no longer do you have to fire up a cluster of mongrels to compensate for the fact that aspects of rails aren't thread safe.
Getting started with Merb
Installing it is simple as Ezra and the crew have made it available as a gem.
sudo gem install merb
Then to create a new application skeleton and get started it's just:
So far it should seem fairly familiar! But how do we start playing with our database?
Connecting to a database
Well because merb doesn't impose any decisions on what ORM to use, you need to install your own. You can stick with ActiveRecord if you like, but let's try something new.
sudo gem install datamapper merb_datamapper do_mysql
We've just installed the DataMapper ORM, the merb interface, and the mysql drivers. Next thing to do to test connecting to a database is setup the connection, and create a model. Update the config/dependencies.rb file and uncomment the following line:
### Uncomment for DataMapper ORM use_orm :datamapper
Next we need to create a config/database.yml file which should be nothing shocking to look at:
--- # This is a sample database file for the DataMapper ORM :development: &defaults :adapter: mysql :database: myapp_development :username: root :password: :host: localhost :test:
and now the joy of creating a model:
script/generate model person --skip-migration
Skip migrations? Are you mad? Do I have to go back creating tables myself? Bear with me here, I'll touch on migrations shortly. Next I'll define some properties on my model in apps/models/person.rb:
Migrations with DataMapper
So we've planned out our model, added in the properties we need. Now if only we could add/retrieve records to/from a table? Creating the required schema in the database is one line:
That's right, DataMapper has automated migrations. It will infer from your model definitions what fields need to exist and create them as appropriate. It'll also take care of primary and foreign keys too! You need to be careful though, because remove a column from the model definition and the next time you automigrate you'll lose that column in the DB too.
So what about the views? What templating systems do I have?
Okay, so creating models isn't too difficult or different from rails. Thankfully the same is true of views too. You can use erubius if you like, but to keep things as close to my version of 'normal' as possible I'm sticking with HAML. So it's really business as usual on this front. Create your views as you normally would, and it should 'just work'
Watch this space for a deeper end-to-end tutorial or screen cast where I'll have a full basic app. Feel free to shoot through any ideas you have.