# Joined search returns NIL ids

**URL:** <https://www.ruby-forum.com/t/joined-search-returns-nil-ids/58646>\
**Category:** Rails\
**Created:** [April 24, 2006, 1:22pm UTC](https://www.ruby-forum.com/t/joined-search-returns-nil-ids/58646 "2006-04-24T13:22:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [April 24, 2006, 1:22pm UTC](https://www.ruby-forum.com/t/joined-search-returns-nil-ids/58646/1 "2006-04-24T13:22:57Z")

</div>

I have two models: assets and makes. Make has many assets and an asset  
belongs to one make.

I am calling this part of my asset controller from a search form with  
the parameters “search” (search term) and “field” (database field  
heading) which then renders a partial with the search results on it.

########################################################

if @params[‘field’] == ‘make\_id’  
@asset\_pages, @assets = paginate :assets,  
:conditions =\> [ “makes.name LIKE ?”,  
‘%’ + @params[‘search’] + ‘%’ ],  
:join =\> “JOIN makes on assets.make\_id =  
makes.id”,  
:per\_page =\> 15  
@mark\_term = @params[‘search’]  
render :partial =\> ‘results’  
end

########################################

I am trying to search for assets having a make with a certain name (name  
is a field in the makes table). The code works and brings back results,  
however the ID of every asset is nil, which is then rendered as 4 (why  
is this?). This means that all the show/edit/destroy links in my list  
view don’t work. Every other attribute of the assets is retrieved  
correctly. What am I doing wrong?

Many thanks,

Chris B.

---

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [April 24, 2006, 3:24pm UTC](https://www.ruby-forum.com/t/joined-search-returns-nil-ids/58646/2 "2006-04-24T15:24:16Z")

</div>

Take a look at the raw SQL in your log file. My guess is that the  
“JOIN” in the :join param is not needed and may be causing problems.

On Monday, April 24, 2006, at 1:22 PM, Chris B. wrote:

> ########################################################  
> end
> 
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)  
> \_Kevin

---

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [April 24, 2006, 6:48pm UTC](https://www.ruby-forum.com/t/joined-search-returns-nil-ids/58646/3 "2006-04-24T18:48:15Z")

</div>

Kevin O. wrote:

> Take a look at the raw SQL in your log file. My guess is that the  
> “JOIN” in the :join param is not needed and may be causing problems.
> 
> On Monday, April 24, 2006, at 1:22 PM, Chris B. wrote:
> 
> > ########################################################  
> > end
> > 
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)  
> > \_Kevin

But then mysql does not know that makes and assets are linked to each  
other:

Mysql::Error: Unknown table ‘makes’ in where clause: SELECT count(\*) AS  
count\_all FROM assets WHERE (makes.name LIKE ‘%tha%’)

There must be a way to do this relationship easily in ruby?

Chris

---

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [April 25, 2006, 2:35am UTC](https://www.ruby-forum.com/t/joined-search-returns-nil-ids/58646/4 "2006-04-25T02:35:39Z")

</div>

Chris B. wrote:

> ########################################
> 
> I am trying to search for assets having a make with a certain name (name  
> is a field in the makes table). The code works and brings back results,  
> however the ID of every asset is nil, which is then rendered as 4 (why  
> is this?). This means that all the show/edit/destroy links in my list  
> view don’t work. Every other attribute of the assets is retrieved  
> correctly. What am I doing wrong?

I think there are two issues here:

1. Unless you add a  
:select =\> ‘assets.\*’  
option to your paginate call you will get the id of the joined makes  
object rather than that of the asset.

2. You are getting 4 because you are calling nil.id, and 4 is the  
internal  
Ruby id of the nil object. Check your code to see why the object on  
which you are calling the id method is nil.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [April 25, 2006, 3:08pm UTC](https://www.ruby-forum.com/t/joined-search-returns-nil-ids/58646/5 "2006-04-25T15:08:11Z")

</div>

Mark Reginald J. wrote:  
are two issues here:

> 1. Unless you add a  
> :select =\> ‘assets.\*’  
> option to your paginate call you will get the id of the joined makes  
> object rather than that of the asset.

I added that and it now works perfectly. Thank you very much!

Chris
