Advanced ActiveRecord Question

WHY DO THIS?

I'm sure a lot of people will chime in with other solutions, some of which may be valid, so I wanted to explain why I need the ability to add an encoded_field with a prototype.

When my application was small, adding fields to the database was easy. You just go into the schema, and add the new field that you need. As the tables got bigger, it became impossible to make changes to the database without having long periods of downtime. As I anticipate the record counts to go into the hundreds of millions, adjusting the table could take a long time (minutes to hours).

This sounds like a bad idea to me. The reason for having a database is to avoid doing things like this - you loose your ability to query these field. You can design a structure which doesn't require you add new table columns which would fit this example. For example having a table "body_attributes" which may have entries like (1, 'eye color') or (2, 'has hair') and then a table called, say, 'body_attribute_values' with entries like (unique_id, 1, "brown") and (unique_id, 2, "no") where the second column is a reference to the id column in "body_attributes" (you could be more complex with the design and allow for body_attribute_values of different types other than strings, like boolean.)

But if you still want to do it: add a class method to ActiveRecord::Base which is this "enocode_fields" method, taking, as you described a hash of default values. For each of those values use the ruby function define_method to add methods (like you described) to the model.

Ryan