# Due date default to n days from now in migration

**URL:** <https://www.ruby-forum.com/t/due-date-default-to-n-days-from-now-in-migration/173772>\
**Category:** Rails\
**Created:** [August 14, 2009, 3:39pm UTC](https://www.ruby-forum.com/t/due-date-default-to-n-days-from-now-in-migration/173772 "2009-08-14T15:39:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/6de8d8/32.png) [@nik](https://www.ruby-forum.com/u/nik)\
**Post date:** [August 14, 2009, 3:39pm UTC](https://www.ruby-forum.com/t/due-date-default-to-n-days-from-now-in-migration/173772/1 "2009-08-14T15:39:57Z")

</div>

hello,how can I set the default value of a column in the migration  
called due\_on with type datetime to n days from now at the time when a  
model is created ?

---

<div class="post-metadata">

**Author:** ![nik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/6de8d8/32.png) [@nik](https://www.ruby-forum.com/u/nik)\
**Post date:** [August 14, 2009, 3:47pm UTC](https://www.ruby-forum.com/t/due-date-default-to-n-days-from-now-in-migration/173772/2 "2009-08-14T15:47:40Z")

</div>

Your migration will have to include a block of code where you go set  
those values yourself for those records where due\_on is null (which  
would be all of them if this is a new field).

---

<div class="post-metadata">

**Author:** ![nik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/6de8d8/32.png) [@nik](https://www.ruby-forum.com/u/nik)\
**Post date:** [August 14, 2009, 3:48pm UTC](https://www.ruby-forum.com/t/due-date-default-to-n-days-from-now-in-migration/173772/3 "2009-08-14T15:48:38Z")

</div>

After the migration, that sort of logic belongs in your model code, in  
an after create callback.

---

<div class="post-metadata">

**Author:** ![nik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/6de8d8/32.png) [@nik](https://www.ruby-forum.com/u/nik)\
**Post date:** [August 14, 2009, 4:17pm UTC](https://www.ruby-forum.com/t/due-date-default-to-n-days-from-now-in-migration/173772/4 "2009-08-14T16:17:25Z")

</div>

Thanks Ar Chron, - one question: why after create is chosen over  
before create in supplying the date that’s n days from now? Could it  
be that this is for validation concerns?

---

<div class="post-metadata">

**Author:** ![nik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/6de8d8/32.png) [@nik](https://www.ruby-forum.com/u/nik)\
**Post date:** [August 14, 2009, 10:38pm UTC](https://www.ruby-forum.com/t/due-date-default-to-n-days-from-now-in-migration/173772/5 "2009-08-14T22:38:05Z")

</div>

In retrospect, before\_create should be fine as it occurs right before  
Base.save if I recall correctly.

But I would choose a callback which occurs after the validations to  
default a value for a new record, unless some validation depends on that  
data. It can get pretty messy with the timings, depending on how your  
models are related.

---

<div class="post-metadata">

**Author:** ![nik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/6de8d8/32.png) [@nik](https://www.ruby-forum.com/u/nik)\
**Post date:** [August 19, 2009, 1:58am UTC](https://www.ruby-forum.com/t/due-date-default-to-n-days-from-now-in-migration/173772/6 "2009-08-19T01:58:21Z")

</div>

Sorry I was out of internet for a few days. – I see, so this is so  
that some sort of predetermined/ non-empty date will always be  
supplied in a business logic point of view.
