# Auto\_complete â€“ create association to anot her model â€“ handle foreign-keys - validation

**URL:** <https://www.ruby-forum.com/t/auto-complete-a-create-association-to-anot-her-model-a-handle-foreign-keys-validation/192214>\
**Category:** Rails\
**Created:** [July 26, 2010, 2:50pm UTC](https://www.ruby-forum.com/t/auto-complete-a-create-association-to-anot-her-model-a-handle-foreign-keys-validation/192214 "2010-07-26T14:50:56Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![bungart1978](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/77aa72/32.png) [@bungart1978](https://www.ruby-forum.com/u/bungart1978)\
**Post date:** [July 26, 2010, 2:50pm UTC](https://www.ruby-forum.com/t/auto-complete-a-create-association-to-anot-her-model-a-handle-foreign-keys-validation/192214/1 "2010-07-26T14:50:56Z")

</div>

Hello,

In the app that I’m currently developing I want to use the  
auto\_complete plugin to create an association from the model “ticket”  
to the model “customer”. In the new ticket form I created the field  
“customer name” as an auto\_complete field. The controller handles the  
lookup on the name field of the customer model.

As I retrieve the name of the customer in the new ticket form this  
parameter is passed to the create action in the ticket controller when  
I hit the create button. In the controller I lookup the name of the  
customer and create the association from the ticket model to the  
customer model in that way.

This works fine for the moment as the name of the customer is unique.  
But what happens when the name of the customer is not unique? How can  
the controller know which customer to associate the model to when  
there are two costumers with the same name?

In my opinion it would be a better solution to retrieve the  
customer.id beside the customer.name. Doing that would it make  
unnecessary to lookup the customer in the controller as the desired id  
of the customer is included in the params hash.

Thinking a bit further I’m also wondering how to make validation work  
on the customer field.

Is there a common solution to that problem? Does anyone have an idea?

Thanks  
Marc
