# \[ANN\] active\_record\_defaults

**URL:** <https://www.ruby-forum.com/t/ann-active-record-defaults/74010>\
**Category:** Rails\
**Created:** [October 29, 2006, 5:38am UTC](https://www.ruby-forum.com/t/ann-active-record-defaults/74010 "2006-10-29T05:38:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jonny](https://www.ruby-forum.com/u/jonny)\
**Post date:** [October 29, 2006, 5:38am UTC](https://www.ruby-forum.com/t/ann-active-record-defaults/74010/1 "2006-10-29T05:38:12Z")

</div>

Greetings,

I just put together a plugin that allows you to easily specify default  
values for AR models. This is useful when your defaults can’t (or you  
don’t  
want to) be specified by the database. I use it to populate attribute  
values  
from user-configurable defaults. Eg:

class Person \< ActiveRecord::Base  
defaults :name =\> ‘My name’, :age =\> 0

default :country do  
Configuration.default\_country  
end  
end

The default values are ignored if the attribute is given a specific  
value.

p = Person.new  
p.name # “My name”

p = Person.new(:name =\> nil)  
p.name # nil

There’s a few more examples here:  
[http://svn.viney.net.nz/things/rails/plugins/active\_record\_defaults/lib/active\_record\_defaults.rb](http://svn.viney.net.nz/things/rails/plugins/active_record_defaults/lib/active_record_defaults.rb)

Install:

ruby script/plugin install  
[http://svn.viney.net.nz/things/rails/plugins/active\_record\_defaults/](http://svn.viney.net.nz/things/rails/plugins/active_record_defaults/)

Cheers,  
-Jonathan.

---

<div class="post-metadata">

**Author:** ![jonny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jonny](https://www.ruby-forum.com/u/jonny)\
**Post date:** [October 29, 2006, 4:13pm UTC](https://www.ruby-forum.com/t/ann-active-record-defaults/74010/2 "2006-10-29T16:13:07Z")

</div>

Posted at [http://agilewebdevelopment.com/plugins/](http://agilewebdevelopment.com/plugins/)  
activerecord\_defaults 🙂

–  
Building an e-commerce site with Rails?  
[http://www.agilewebdevelopment.com/rails-ecommerce](http://www.agilewebdevelopment.com/rails-ecommerce)

---

<div class="post-metadata">

**Author:** ![jonny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jonny](https://www.ruby-forum.com/u/jonny)\
**Post date:** [October 29, 2006, 4:33pm UTC](https://www.ruby-forum.com/t/ann-active-record-defaults/74010/3 "2006-10-29T16:33:09Z")

</div>

Clever Jonathan V. but why not at state defaults at migration. ?

On 10/29/06, Benjamin C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end
> 
> >

–  
Never be afraid to try something new. Remember, amateurs built the ark;  
professionals built the Titanic!

---

<div class="post-metadata">

**Author:** ![jonny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jonny](https://www.ruby-forum.com/u/jonny)\
**Post date:** [October 29, 2006, 5:39pm UTC](https://www.ruby-forum.com/t/ann-active-record-defaults/74010/4 "2006-10-29T17:39:19Z")

</div>

Jonathan, I have a question.

Did you implement through after\_initialize? If so this can have the  
negative side-effect of setting nil values to the default values  
after Find?

Or are you applying the defaults only through calls to New?

J

---

<div class="post-metadata">

**Author:** ![jonny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jonny](https://www.ruby-forum.com/u/jonny)\
**Post date:** [November 7, 2006, 10:44am UTC](https://www.ruby-forum.com/t/ann-active-record-defaults/74010/5 "2006-11-07T10:44:06Z")

</div>

You can only define static defaults with migrations. This plugin allows  
the  
defaults to be determined when the model object is created. I use this  
to  
allow users to implement their own default values.

-Jonathan.

---

<div class="post-metadata">

**Author:** ![jonny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jonny](https://www.ruby-forum.com/u/jonny)\
**Post date:** [November 7, 2006, 10:52am UTC](https://www.ruby-forum.com/t/ann-active-record-defaults/74010/6 "2006-11-07T10:52:14Z")

</div>

Thanx Jonathan. Good stuff.

I have a class, with a great number of attributes - would it be  
possible to add a method call to assign all default values?

something like?

class Person \< ActiveRecord::Base  
defaults :set\_defaults

```
def set_defaults
 name = 'My name'
 city = 'My city'

```

end

?

Jodi

---

<div class="post-metadata">

**Author:** ![jonny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jonny](https://www.ruby-forum.com/u/jonny)\
**Post date:** [November 7, 2006, 10:53am UTC](https://www.ruby-forum.com/t/ann-active-record-defaults/74010/7 "2006-11-07T10:53:25Z")

</div>

I’ve just updated the plugin to allow you to do this.

class Person \< ActiveRecord::Base  
def defaults  
# do whatever you like in here  
end  
end

I could remove the class methods for defining defaults and move to this  
method of simply having a defaults method on the model. This gives a bit  
more flexibility, but does make specifying simple defaults for one or  
two  
attributes take an extra line or two of code.

class Person \< AR  
defaults :name =\> ‘Jonathan’  
end

vs

class Person \< AR  
def defaults  
self.name = ‘Jonathan’  
end  
end

Thoughts?

-Jonathan.

---

<div class="post-metadata">

**Author:** ![jonny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jonny](https://www.ruby-forum.com/u/jonny)\
**Post date:** [November 7, 2006, 10:44am UTC](https://www.ruby-forum.com/t/ann-active-record-defaults/74010/8 "2006-11-07T10:44:06Z")

</div>

No, after\_initialize is not used. I simply aliased the original  
initialize  
method, and called it from within the one I defined.

Existing records will remain unchanged. The default values are only used  
when creating new records.

-Jonathan.
