in Applications, django, python, Rails, Ruby

Rails vs Grails vs Django models

Coming back to Rails after being away from some time in Django land I discovered a huge difference in how Rails, Grails and Django treats your models. In Django and Grails you can look at a model class and see all the properties it has:

class Organization(models.Model):
    name = models.CharField(max_length=255)
    url = models.URLField(verify_exists=False)
    orgtype = models.ForeignKey(OrgType)

The same model class in Rails typically looks like this:

class Organization < ActiveRecord::Base
    belongs_to :OrgType

…and in Grails it is more specific like Django:

class Organization {
  String name
  String url
  static belongsTo = OrgType
  OrgType orgtype

It took me a while to remember that in Rails, parts of the model design is actually stored in the database schema instead of in the Ruby code. Peculiar don’t you think, given that everything else in a Rails app is nicely declared in Ruby code? There are of course benefits to both approaches, but I have started adding comments in the Rails model classes to be able to remember what properties they have without peeking in the Db. Typically I have a number of half-baked projects on my laptop and from time to time I forget what they do and these comments help me remember.

Check out more examples here:

  1. Besides annotated models you can use DataMapper (, created to be Merb’s ORM, but can be used with Rails too. It uses a different pattern from ActiveRecord, and using it you would have something like:

    class Organization
      include DataMapper::Resource
      property :id, Serial, :key => true
      property :name, String, :nullable => false
      property :url, String
      belongs_to :org_type

    It’s just beautiful, IMHO :) Rails integration isn’t perfect, but I believe it will be seamless with Rails 3.0. It’s worth a look.


  2. And although Active Record is the de facto Rails-way, it is not alone anymore. You can use Data Mapper and Sequel, albeit with small tweaks. Rails 3.0 will bring ORM agnosticism, so you’re free to choose. Now, Active Record is still much easier specifically for green-field projects. It was not made for legacy integration. It was never a goal to reach more than 80% of the needs – remember Paretto ;-)

Comments are closed.