Select only if exist?

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
It is in Hebrew

On 23 Aug 2008, at 11:01, [email protected] 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

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

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

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. [email protected]

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.

Thanks for the tip, Phlip

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