# Select only if exist?

**URL:** <https://www.ruby-forum.com/t/select-only-if-exist/146640>\
**Category:** Rails\
**Created:** [August 23, 2008, 12:01pm UTC](https://www.ruby-forum.com/t/select-only-if-exist/146640 "2008-08-23T12:01:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 23, 2008, 12:01pm UTC](https://www.ruby-forum.com/t/select-only-if-exist/146640/1 "2008-08-23T12:01:34Z")

</div>

I have a table for neighborhoods with a name and an id  
Also have a table for assets, each asset have a neighborhood\_id  
Asset model =\> belongs to neighborhood  
Neighborhood model =\> has many assets  
I have a page where I list all assets, and a search form to find  
assets by selecting from a list of neighborhoods.  
The list of neighborhoods is taken from the neighborhoods table

@list\_all\_neighborhoods = Neighborhood.find(:all, :order =\> “name”)  
\<%= collection\_select(:asset, :neighborhood\_id,  
@list\_all\_neighborhoods, :id, :name) %\>

Now if I search for a neighborhood that do not exist in one of the  
assets I get a message like “no results try something else”

I want to eliminate that message and show on the search form only  
neighborhoods that actually exist in the assets table.

I understand that i need to take the @list\_all\_neighborhoods array and  
check each record to see if exist in the assets table

But I can’t make this work

I appreciate any help

Thanks

Erez

The website is online here [http://www.5354.co.il](http://www.5354.co.il)  
It is in Hebrew

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 23, 2008, 4:28pm UTC](https://www.ruby-forum.com/t/select-only-if-exist/146640/2 "2008-08-23T16:28:28Z")

</div>

On 23 Aug 2008, at 11:01, removed\_email\_address@domain.invalid wrote:

> \<%= collection\_select(:asset, :neighborhood\_id,  
> @list\_all\_neighborhoods, :id, :name) %\>

If you add a :joins =\> :assets then rails will add a left join (that  
will discard neighbourhoods with no assets)

Fred

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 23, 2008, 6:14pm UTC](https://www.ruby-forum.com/t/select-only-if-exist/146640/3 "2008-08-23T18:14:54Z")

</div>

Frederick C. wrote:

> If you add a :joins =\> :assets then rails will add a left join (that  
> will discard neighbourhoods with no assets)
> 
> Fred

HI Fred,

Methinks you meant to say “inner join”.

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 23, 2008, 7:11pm UTC](https://www.ruby-forum.com/t/select-only-if-exist/146640/4 "2008-08-23T19:11:10Z")

</div>

erez.bens wrote:

> @list\_all\_neighborhoods = Neighborhood.find(:all,  
> :order =\> “name”,

Important programming tip: All editors ship with tabs turned on, and all  
programmers should never ever use tabs. They are a legacy of  
programming’s  
infancy, and of editors that cannot smartly indent and unindent code!

–  
Phlip

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 23, 2008, 6:43pm UTC](https://www.ruby-forum.com/t/select-only-if-exist/146640/5 "2008-08-23T18:43:40Z")

</div>

Thank you Frad  
This is working great

@list\_all\_neighborhoods = Neighborhood.find(:all,  
:order =\> “name”,  
:joins =\> :assets,  
:group =\> :neighborhood\_id)

On 23 ××•×’×•×¡×˜, 17:11, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 23, 2008, 7:26pm UTC](https://www.ruby-forum.com/t/select-only-if-exist/146640/6 "2008-08-23T19:26:22Z")

</div>

On 23 Aug 2008, at 17:14, Phillip K. wrote:

> Frederick C. wrote:
> 
> > If you add a :joins =\> :assets then rails will add a left join (that  
> > will discard neighbourhoods with no assets)
> > 
> > Fred
> 
> HI Fred,
> 
> Methinks you meant to say “inner join”.

Of course, brain switched off.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 23, 2008, 8:19pm UTC](https://www.ruby-forum.com/t/select-only-if-exist/146640/7 "2008-08-23T20:19:35Z")

</div>

Thanks for the tip, Phlip

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 23, 2008, 8:52pm UTC](https://www.ruby-forum.com/t/select-only-if-exist/146640/8 "2008-08-23T20:52:07Z")

</div>

Phlip wrote:

> erez.bens wrote:
> 
> > @list\_all\_neighborhoods = Neighborhood.find(:all,  
> > :order =\> “name”,
> 
> Important programming tip: All editors ship with tabs turned on, and all  
> programmers should never ever use tabs. They are a legacy of  
> programming’s  
> infancy, and of editors that cannot smartly indent and unindent code!
> 
> –  
> Phlip

Oh, puhlease don’t start this. There is, in my opinion, no more  
worthless argument than about tabs versus spaces.

Peace,  
Phillip
