# Lookup from related tables

**URL:** <https://www.ruby-forum.com/t/lookup-from-related-tables/98657>\
**Category:** Rails\
**Created:** [January 30, 2006, 1:19am UTC](https://www.ruby-forum.com/t/lookup-from-related-tables/98657 "2006-01-30T01:19:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [January 30, 2006, 1:19am UTC](https://www.ruby-forum.com/t/lookup-from-related-tables/98657/1 "2006-01-30T01:19:05Z")

</div>

I have been working on this for 3 days and haven’t been able to solve  
it.

I have 2 tables…

clients  
belongs\_to: case\_managers

case\_managers  
has\_many: clients

When entering new clients, I need to have some type of select box that  
allows me to pick the case\_manager (by name) so that the ‘id’ field from  
the case\_manager table is inserted into clients.case\_manager\_id field.

I cannot figure out how to do that with either auto\_complete or select  
functions. This isn’t exactly covered in Agile book.

Can anyone point me to an example of how this is done?

Thanks

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [January 30, 2006, 1:58am UTC](https://www.ruby-forum.com/t/lookup-from-related-tables/98657/2 "2006-01-30T01:58:14Z")

</div>

In your clients controller and the new action, load your casemanagers:

client\_controller.rb:

def new  
@client = Client.new  
@case\_managers = CaseManager.find(:all)  
end

views/clients/\_form.rhtml:  
\<%= options = [[‘Select a Case Manager’, ‘’]] + @case\_managers.collect {  
|cm| [cm.name, cm.id] }  
select ‘client’, ‘case\_manager\_id’, options %\>

Something like that should work for you. Not tested though just off the  
top  
of my head.

Bob S.  
[http://www.railtie.net/](http://www.railtie.net/)

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [January 30, 2006, 7:00am UTC](https://www.ruby-forum.com/t/lookup-from-related-tables/98657/3 "2006-01-30T07:00:39Z")

</div>

Bob - thanks for taking the time to review my issue

I’m certain that I followed your instruction but I get an error that I  
can’t resolve…I’ve run into this error many times in all the various  
methods that I’ve tried.

NoMethodError in Clients#new  
Showing app/views/clients/\_form.rhtml where line #21 raised:

You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occured while evaluating nil.collect

Extracted source (around line #21):

18:  
19:

Case Manager  
  
20: \<%# text\_field\_with\_auto\_complete(:case\_manager, :name) %\>  
21: \<%= options = [[‘Select a Case Manager’, ‘’]] +  
@case\_managers.collect { |cm| [cm.name, cm.id] }  
22: select ‘client’, ‘case\_manager\_id’, options %\>  
23:  
24:

ID

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [January 30, 2006, 8:10am UTC](https://www.ruby-forum.com/t/lookup-from-related-tables/98657/4 "2006-01-30T08:10:19Z")

</div>

Looking at your code, it appears your case\_manager is actually casemgr  
in  
the db. Adjust your naming scheme appropriately. Remember, convention  
over  
configuration.

Bob S.  
[http://www.railtie.net/](http://www.railtie.net/)

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [January 30, 2006, 8:43am UTC](https://www.ruby-forum.com/t/lookup-from-related-tables/98657/5 "2006-01-30T08:43:29Z")

</div>

No, the ‘casemgr’ thing you are seeing is only labels (not active code  
areas) and I never bothered changing the labels - I have now.

I have solved that issue.

Thanks to your help

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [January 30, 2006, 7:24am UTC](https://www.ruby-forum.com/t/lookup-from-related-tables/98657/6 "2006-01-30T07:24:45Z")

</div>

Solved (finally) - thanks Bob

I had to use ‘case\_manager.collect’ - not ‘case\_managers.collect’

Does this deserve a wiki page? This took days to work out.

Craig
