# Update FK\_ID at validation/save time in activerecor

**URL:** <https://www.ruby-forum.com/t/update-fk-id-at-validation-save-time-in-activerecor/190468>\
**Category:** Ruby\
**Created:** [June 21, 2010, 6:59am UTC](https://www.ruby-forum.com/t/update-fk-id-at-validation-save-time-in-activerecor/190468 "2010-06-21T06:59:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![michbukana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@michbukana](https://www.ruby-forum.com/u/michbukana)\
**Post date:** [June 21, 2010, 6:59am UTC](https://www.ruby-forum.com/t/update-fk-id-at-validation-save-time-in-activerecor/190468/1 "2010-06-21T06:59:36Z")

</div>

I have:

* * *

class Customer \< ActiveRecord::Base  
belongs\_to :Customer\_Type  
end

class Customer\_Type \< ActiveRecord::Base  
has\_many :Customers  
end

* * *

Is there a built-in way to auto populate customer\_type\_id  
based on the “Human name” of the Customer\_Type\_ID?

* * *

## c = Customer.new( :name =\> “John D.”, :customer\_type\_name =\> “Wholesale” )

# c[:Customer\_Type\_ID] == 10 (if 10 is the id for the “Wholesale”

customer type)

---

<div class="post-metadata">

**Author:** ![michbukana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@michbukana](https://www.ruby-forum.com/u/michbukana)\
**Post date:** [June 21, 2010, 10:09am UTC](https://www.ruby-forum.com/t/update-fk-id-at-validation-save-time-in-activerecor/190468/2 "2010-06-21T10:09:56Z")

</div>

On Mon, Jun 21, 2010 at 7:59 AM, Eduardo B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## customer type)
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

ActiveRecord questions are better placed on the Rails mailing list,  
but here’s a hint:

type = CustomerType.find\_by\_name(“Wholesale”)

Ammar
