# Default value for model var

**URL:** <https://www.ruby-forum.com/t/default-value-for-model-var/63162>\
**Category:** Rails\
**Created:** [June 12, 2006, 9:25pm UTC](https://www.ruby-forum.com/t/default-value-for-model-var/63162 "2006-06-12T21:25:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonathan2000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/fbc32d/32.png) [@jonathan2000](https://www.ruby-forum.com/u/jonathan2000)\
**Post date:** [June 12, 2006, 9:25pm UTC](https://www.ruby-forum.com/t/default-value-for-model-var/63162/1 "2006-06-12T21:25:39Z")

</div>

I know this has got to be a really dumb question, but here goes anyway.

I’ve got a model with an associated table. One of the columns in the  
table is ‘date\_created’, which I want to be set to the date/time each  
row was added to the table.

Seems to me that setting this in the initialize method for the model  
would be the right thing to do,…

class MyThing \< ActiveRecord::Base  
def initialize  
@date\_created = DateTime.now  
end  
…

But that throws other errors since the controller is calling the class  
with additional arguments from the form entry that started the process.

@thing = MyThing.new(params[:thing])

I know I must be missing something pretty basic, but any help getting me  
pointed in the right direction would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![jonathan2000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/fbc32d/32.png) [@jonathan2000](https://www.ruby-forum.com/u/jonathan2000)\
**Post date:** [June 12, 2006, 9:41pm UTC](https://www.ruby-forum.com/t/default-value-for-model-var/63162/2 "2006-06-12T21:41:57Z")

</div>

I knew that as soon as I asked, I’d find the answer on my own.

created\_at/created\_on; Not exactly what I was looking for, but it’ll  
serve the purpose for now.

However, any info on the more general task of setting default values for  
model variables would still be appreciated.

---

<div class="post-metadata">

**Author:** ![jonathan2000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/fbc32d/32.png) [@jonathan2000](https://www.ruby-forum.com/u/jonathan2000)\
**Post date:** [June 13, 2006, 1:07am UTC](https://www.ruby-forum.com/t/default-value-for-model-var/63162/3 "2006-06-13T01:07:10Z")

</div>

> def initialize  
> I know I must be missing something pretty basic, but any help getting me  
> pointed in the right direction would be greatly appreciated.

You already found created\_at/on and it’s friend updated\_at/on, but also  
look into before\_save() and before\_create().

-philip

---

<div class="post-metadata">

**Author:** ![jonathan2000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/fbc32d/32.png) [@jonathan2000](https://www.ruby-forum.com/u/jonathan2000)\
**Post date:** [June 13, 2006, 2:05am UTC](https://www.ruby-forum.com/t/default-value-for-model-var/63162/4 "2006-06-13T02:05:47Z")

</div>

That’s exactly what I was looking for. Thanks!
