Forum: Ruby on Rails Default values

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Michael D. (Guest)
on 2006-06-02 07:36
(Received via mailing list)
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.
Dave T. (Guest)
on 2006-06-03 20:50
(Received via mailing list)
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 :)

On Thursday, June 01, 2006, at 8:34 PM, Michael D. wrote:
>     default_value :rating, {|m| m.genre.default_rating }
>   end

--Dave T. - Password Manager + AutoFill for Mac OS X - Email list management - Apps for your hand-held
This topic is locked and can not be replied to.