Re: select only if exist?

Frederick Cheung
Sat, 23 Aug 2008 14:11:23 -0000

On 23 Aug 2008, at 11:01, erez.bens-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
wrote:

>
> 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) %>
>

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

Fred
> 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
>
> >