views:

44

answers:

3

In Django, you fully describe your models in models.py. In Rails with ActiveRecord, you describe part of a model in in the /models directory, and part of it in migrations. Then ActiveRecord introspects model properties from the existing database tables.

But I find migrations, columns, and tables to be a headache.

How can I do like Django -- just declare all model properties instead of introspecting them from the database tables?

And for extra credit, explain where and why this would be a bad idea. :)

+1  A: 

Try the auto_migrations plugin. I don't think it's a bad idea for development, but I would switch to migrations after going to production when there is critical data in the database.

You may also be interested in replacing ActiveRecord with DataMapper, which works in Rails 3. It has the style you are talking about, with the description of the data fields of a model in the model code instead of a separate database schema file.

mckeed
+1  A: 

If you hate on Migrations, try going NoSQL. No migrations!

So you'd just add properties to your document when you need them. In your code, handle the fact that they may not exist and bam!

I took the following model definition (notice you don't inherit form activerecord) from a blog about tekpub Also recommend the Herding Code podcast

class Production

  include MongoMapper::Document

  key :title, String, :required => true
  key :slug, String, :unique => true, :required => true, :index => true
  key :description, String
  key :notes, String
  key :price, BigDecimal, :numeric => true
  key :released_at, Date, :default => Date.today
  key :default_height, String, :default => '600'
  key :default_width, String, :default => '1000'
  key :quotes, String

  #royalty info
  key :producers, String

  timestamps!
end
Jesse Wolgamott
+1  A: 

I think DataMapper is what you are asking for. Once set up, you'd either use DataMapper.auto_migrate! or DataMapper.auto_upgrade!. The former will drop tables if they exists before creating them, thus destroying any data. That would be bad for production. The latter is how you avoid losing data, and should be just fine for production.

Without knowing exactly what its doing, I'd guess it's inspecting tables during startup to determine whether to make database changes. That can drag down start up time, especially with a lot of models/tables. Which is actually one of the good reasons to consider NoSQL - specifically Mongo as mentioned above. It's fast. Really fast, and thus the start up cost is much, much less. MongoMapper is the way to go. The tekpub blog post is a must read.

I first heard about DataMapper in reading about Merb, so it makes sense that it's in rails 3. I don't know whether you may be able to get it working in rails 2.x.

joshc