# Default values

**URL:** <https://www.ruby-forum.com/t/default-values/95468>\
**Category:** Rails\
**Created:** [June 2, 2006, 5:36am UTC](https://www.ruby-forum.com/t/default-values/95468 "2006-06-02T05:36:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdeering](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/278dde/32.png) [@mdeering](https://www.ruby-forum.com/u/mdeering)\
**Post date:** [June 2, 2006, 5:36am UTC](https://www.ruby-forum.com/t/default-values/95468/1 "2006-06-02T05:36:07Z")

</div>

What do you think about doing this in a model?

class GarageSaleItem  
default\_value :price, 0.1  
end

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]  
end

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

And then this:

class Movie  
default\_value :rating, Proc.new {|m| m.genre.default\_rating }  
end

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  
end

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.  
[http://www.mdaines.com](http://www.mdaines.com)

---

<div class="post-metadata">

**Author:** ![mdeering](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/278dde/32.png) [@mdeering](https://www.ruby-forum.com/u/mdeering)\
**Post date:** [June 3, 2006, 6:50pm UTC](https://www.ruby-forum.com/t/default-values/95468/2 "2006-06-03T18:50:36Z")

</div>

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, Proc.new {|m| m.genre.default_rating }
> 
> ```
> 
> end  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Cheers!  
–Dave T.  
[http://1passwd.com](http://1passwd.com) - Password Manager + AutoFill for Mac OS X  
[http://devlists.com](http://devlists.com) - Email list management  
[http://palmsphere.com](http://palmsphere.com) - Apps for your hand-held
