# Trouble with the results of a dynamically generated find() method

**URL:** <https://www.ruby-forum.com/t/trouble-with-the-results-of-a-dynamically-generated-find-method/130523>\
**Category:** Rails\
**Created:** [February 29, 2008, 1:05am UTC](https://www.ruby-forum.com/t/trouble-with-the-results-of-a-dynamically-generated-find-method/130523 "2008-02-29T01:05:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![brad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/59ef9b/32.png) [@brad](https://www.ruby-forum.com/u/brad)\
**Post date:** [February 29, 2008, 1:05am UTC](https://www.ruby-forum.com/t/trouble-with-the-results-of-a-dynamically-generated-find-method/130523/1 "2008-02-29T01:05:23Z")

</div>

Hi all,

So I have two tables:

- apps (short for applicants)  
has\_and\_belongs\_to\_many :locations

- locations  
has\_and\_belongs\_to\_many :apps

In my system, a user searches for applicants by the locations to which  
they are associated, by using a dropdown menu that includes the names  
of locations (and sends to the method that returns the results the id  
of the selected location).

So, in the application for “joe,” joe has indicated that he wants to  
live in “ethiopia” and “cambridge”. If I search for all apps  
associated to “ethiopia,” joe DOES result. That’s good. But, in the  
result, in joe’s row, only ethiopia appears. Where’s cambridge?

Here’s my find (which I’ve canned to look for ethiopia by it’s id of  
41):  
@apps = App.find(:all, :include =\> :locations, :conditions =\>  
[‘locations.id = ?’, 41])

Here’s the result, in script/console:  
=\> [#\<App:0x368bcf0 @locations=[#\<Location:0x3689f2c  
@attributes={“status”=\>“open”, “name”=\>“Ethiopia”, “id”=\>“41”}\>],  
@attributes={“prefix”=\>“Mr”, “first\_name”=\>“joe”,  
“last\_name”=\>“bloggs”}\>]

So, my find is clearly limiting my result to show only the location  
that was searched for in joe’s row. But I want to show joe’s row  
complete with all his associations, including joe’s association to  
cambridge. I just want to be able to search for joe by his  
association to ‘ethiopia.’

Can anyone help me out?

Thanks,  
Brad

---

<div class="post-metadata">

**Author:** ![brad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/59ef9b/32.png) [@brad](https://www.ruby-forum.com/u/brad)\
**Post date:** [February 29, 2008, 3:20am UTC](https://www.ruby-forum.com/t/trouble-with-the-results-of-a-dynamically-generated-find-method/130523/2 "2008-02-29T03:20:26Z")

</div>

brad wrote:

> Here’s my find (which I’ve canned to look for ethiopia by it’s id of  
> 41):  
> @apps = App.find(:all, :include =\> :locations, :conditions =\>  
> [‘locations.id = ?’, 41])
> 
> Can anyone help me out?
> 
> Thanks,  
> Brad

@apps = App.find(:all, :include =\> :locations, :conditions =\>  
[‘locations.id in ?’, [41, 54].join(’,’)])

hth

ilan

---

<div class="post-metadata">

**Author:** ![brad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/59ef9b/32.png) [@brad](https://www.ruby-forum.com/u/brad)\
**Post date:** [February 29, 2008, 10:46am UTC](https://www.ruby-forum.com/t/trouble-with-the-results-of-a-dynamically-generated-find-method/130523/3 "2008-02-29T10:46:02Z")

</div>

you can pass “true” as an argument to your association to force it to  
reload the values from the database.

For example:

App.find(:all, :conditions =\> [‘locations.name = ?’, ‘Ethiopia’],  
:include =\> “locations”).each do |user|  
puts “user: #{user.name} num locations #{user.locations(true).length}”  
end

Mike
