# Is it possible to set default value in a migration based upon another field's value?

**URL:** <https://www.ruby-forum.com/t/is-it-possible-to-set-default-value-in-a-migration-based-upon-another-fields-value/193390>\
**Category:** Rails\
**Created:** [August 16, 2010, 11:34pm UTC](https://www.ruby-forum.com/t/is-it-possible-to-set-default-value-in-a-migration-based-upon-another-fields-value/193390 "2010-08-16T23:34:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [August 16, 2010, 11:34pm UTC](https://www.ruby-forum.com/t/is-it-possible-to-set-default-value-in-a-migration-based-upon-another-fields-value/193390/1 "2010-08-16T23:34:36Z")

</div>

I’m familiar with setting a field’s default in the create\_table within  
a migration via :default =\> some value. However, what I’m looking for  
is to be able to set the default value based upon the value from a  
different field/column. I’m not sure if this is possible or not, so  
any help would be greatly appreciated. The exact scenario is that I’m  
using devise for my authentication and I’ve extended it to have a  
login\_id field in addition to the email field. I’d like to default it  
to the value of the email field when the user first creates their  
account and they can later choose to change its value if they decide  
they would rather use some value other than their email address. I  
know I can override the controller that devise uses to set this value,  
but I’m trying to see if I have any options that will not require me  
to create an overridden controller.

Thanks in advance for any guidance and help.

Chris

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [August 17, 2010, 12:06am UTC](https://www.ruby-forum.com/t/is-it-possible-to-set-default-value-in-a-migration-based-upon-another-fields-value/193390/2 "2010-08-17T00:06:13Z")

</div>

On Aug 16, 2010, at 3:18 PM, Chris wrote:

> know I can override the controller that devise uses to set this value,  
> but I’m trying to see if I have any options that will not require me  
> to create an overridden controller.
> 
> Thanks in advance for any guidance and help.
> 
> Chris

Perhaps you can use a before\_create callback on the user (account?)  
model.

class User  
before\_create :default\_login\_id

private  
def default\_login\_id  
self.login\_id ||= self.email  
end  
end

-Rob

Rob B.  
removed\_email\_address@domain.invalid [http://AgileConsultingLLC.com/](http://AgileConsultingLLC.com/)  
removed\_email\_address@domain.invalid [http://GaslightSoftware.com/](http://GaslightSoftware.com/)

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [August 17, 2010, 4:54am UTC](https://www.ruby-forum.com/t/is-it-possible-to-set-default-value-in-a-migration-based-upon-another-fields-value/193390/3 "2010-08-17T04:54:16Z")

</div>

On Mon, Aug 16, 2010 at 2:18 PM, Chris [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m familiar with setting a field’s default in the create\_table within  
> a migration via :default =\> some value. However, what I’m looking for  
> is to be able to set the default value based upon the value from a  
> different field/column. I’m not sure if this is possible or not, so  
> any help would be greatly appreciated.

You can put active record code into a migration:

default = Model.find(:first).name rescue nil

:default =\> default

–  
Greg D.  
[destiney.com](http://destiney.com) | [gregdonald.com](http://gregdonald.com)
