Default values


What do you think about doing this in a model?

class GarageSaleItem
default_value :price, 0.1

A new GarageSaleItem (with nil for price) will cost 10 cents, the
default value. If a price is set, it’ll use that price. If the record
is saved and no price is yet set, 10 cents is saved for the price.
You can do this in the schema, I guess, but can you do this easily?

class Mixture
serialize :color
default_value :color, [255, 0, 0]

As you can see, all mixtures are red mixtures by default.

And then this:

class Movie
default_value :rating, {|m| m.genre.default_rating }

Clearly this doesn’t convincingly map to a real situation, but it’s
clear what’s going on. Instead of using a literal value, we’re
calling a proc. Symbols would work they same way they do with filters.

Also, this:

class MultiFaceted
default_value [:foo, :bar], true

I have a little plugin for this and I’m using it, but is this a
pattern people use? Is it even a reasonable pattern? Why or why not?

Michael D.


At first glance this looks neat. I normally have class methods for
constructing the objects when needed or use the AR after_create
callback. While this allows me to have different “default values”
depending on the situation, your syntax is more succinct.

I wouldn’t mind giving your plugin a go. Release your plugin into the
wild world and let it fend for itself :slight_smile:

On Thursday, June 01, 2006, at 8:34 PM, Michael D. wrote:

default_value :rating, {|m| m.genre.default_rating }


–Dave T. - Password Manager + AutoFill for Mac OS X - Email list management - Apps for your hand-held