Saturday, December 13, 2008

merb and sequel

Recently I needed to order some records by a mathematical formula, and I couldn't figure out a way to do it in DM (although it looks like the idea was well received and might make it into a future relase). I'd heard some great things about Sequel, so I decided to take a look and see what possibilities I had there.

If you haven't read anything about Sequel, head on over to its rubyforge page at There's a good amount of information from basics to how you can implement complex associations between models. There's even a link to Lori Holden's MerbCamp presentation.

Sequel piqued my interest in many ways: it uses the Active Record Pattern to create methods from column names, but you can also do a more DataMapper style definition of columns inside your model:

You can use Post.create_table! as a sort of automigrate (it will drop and recreate the table). The usage of set_schema is more for test code and experimenting, and I found it very useful when developing sequel_polymorphic and sequel_taggable.

So, while its easy enough to generate a new merb core app and customize it as you like, if you're going to be generating a lot of apps with the same configurations, its worth learning how to build a stack. Currently its mostly a manual process: you build a gem with a certain structure by hand, and then use your stack with merb-gen. Eventually this will probably be an easily streamlined process, but for now its still up to you to put things together. You can read more about the structure here and take a look at merb-sequel-stack as well.

If you're looking to get a bit closer to your DB and want a lot of flexibility and power, you should really take Sequel for a test drive. Its current maintainer, Jeremy Evans, has been a ton of help with answering dozens of questions in #sequel, even when its to answer something he doesn't agree with like polymorphic associations, which I'll go into next time.

1 comment:

Matt said...

nice article. Just a quick addition, with the help of the guys we release merb_sequel 0.9.14 and are working on a 1.0 release.

They will also write the sequel section on the Merb Open Source Book.

- Matt