# Best place to initialize a database column value

**URL:** <https://www.ruby-forum.com/t/best-place-to-initialize-a-database-column-value/114369>\
**Category:** Rails\
**Created:** [September 11, 2007, 10:24pm UTC](https://www.ruby-forum.com/t/best-place-to-initialize-a-database-column-value/114369 "2007-09-11T22:24:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![JodyB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@JodyB](https://www.ruby-forum.com/u/JodyB)\
**Post date:** [September 11, 2007, 10:24pm UTC](https://www.ruby-forum.com/t/best-place-to-initialize-a-database-column-value/114369/1 "2007-09-11T22:24:25Z")

</div>

I have a database column named ‘status’ in my ‘orders’ table. When an  
order placed, I’d like to initialize this column value to be ‘in  
progress’. Is it best to do this in:

A) the database migration  
B) the database itself  
C) in before\_create

My personal preference is to do it in the before\_create. This way it  
is very evident to me when I look at the model as to what the value is  
going to be initialized to. Are there performance issues with doing  
this?

Jody Baty  
Lead Developer  
my Curriculum Analysis Tools ([www.mycatshq.com](http://www.mycatshq.com))

---

<div class="post-metadata">

**Author:** ![JodyB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@JodyB](https://www.ruby-forum.com/u/JodyB)\
**Post date:** [September 12, 2007, 6:37am UTC](https://www.ruby-forum.com/t/best-place-to-initialize-a-database-column-value/114369/2 "2007-09-12T06:37:07Z")

</div>

Hi Jody,

i am using Oracle for back-end storage, so all my default values for  
columns are in sql scripts, which is separate folder,  
and i don’t think introduction of default value in any of the 3 places  
you mentioned will lead to performance problem.

(Worst case: if it affects also it should be very very negligible, but  
i don’t have benchmark results, personally i feel, it will not)

---

<div class="post-metadata">

**Author:** ![JodyB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@JodyB](https://www.ruby-forum.com/u/JodyB)\
**Post date:** [September 12, 2007, 6:20pm UTC](https://www.ruby-forum.com/t/best-place-to-initialize-a-database-column-value/114369/3 "2007-09-12T18:20:48Z")

</div>

JodyB wrote:

> I have a database column named ‘status’ in my ‘orders’ table. When an  
> order placed, I’d like to initialize this column value to be ‘in  
> progress’. Is it best to do this in:
> 
> A) the database migration  
> B) the database itself  
> C) in before\_create
> 
> My personal preference is to do it in the before\_create. This way it  
> is very evident to me when I look at the model as to what the value is  
> going to be initialized to. Are there performance issues with doing  
> this?

I don’t think there are performance issues to worry about. The  
before\_create callback imposes a bit of overhead, but it’s nothing to  
worry about. I’d go with the before\_create approach, since I like to  
keep as much of my biz logic in Ruby as I can. Makes it easier to tell  
what’s going on, it’s more portable, and I’m better at Ruby than SQL.

Note that if you are using before\_create callback and you make an object  
with #new instead of #create, you can have an object floating around  
that hasn’t had that attribute initialized as you might expect. For  
those situtations, I often do lazy initialization on accessors:

def foo  
self.attributes[:foo] ||= “default value”  
end

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![JodyB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@JodyB](https://www.ruby-forum.com/u/JodyB)\
**Post date:** [September 12, 2007, 8:13pm UTC](https://www.ruby-forum.com/t/best-place-to-initialize-a-database-column-value/114369/4 "2007-09-12T20:13:54Z")

</div>

On 9/11/07, JodyB [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> is very evident to me when I look at the model as to what the value is  
> going to be initialized to. Are there performance issues with doing  
> this?

To give you yet another option, there’s a dead simple little plugin  
called default\_values here:

[Rubaidh.com is for sale | HugeDomains](http://svn.rubaidh.com/plugins/trunk/default_values)

You then just declare default values in your model:

class Order \< ActiveRecord::Base  
default\_values :status =\> ‘in progress’  
end

---

<div class="post-metadata">

**Author:** ![JodyB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@JodyB](https://www.ruby-forum.com/u/JodyB)\
**Post date:** [September 17, 2007, 1:21pm UTC](https://www.ruby-forum.com/t/best-place-to-initialize-a-database-column-value/114369/5 "2007-09-17T13:21:37Z")

</div>

Thanks, I’m certainly going to give this plugin a try. It’s exactly  
what I’m looking for: expressive and easy to implement.

Jody

---

<div class="post-metadata">

**Author:** ![JodyB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@JodyB](https://www.ruby-forum.com/u/JodyB)\
**Post date:** [September 12, 2007, 6:46pm UTC](https://www.ruby-forum.com/t/best-place-to-initialize-a-database-column-value/114369/6 "2007-09-12T18:46:29Z")

</div>

I’m using DEFAULT attribute (set to ‘pending’) on a column inside  
mysql.
