Forum: Ruby on Rails Joined search returns NIL ids

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7bbc5c78011f20a36ee0cffa15b575ef?d=identicon&s=25 Chris Birkinshaw (Guest)
on 2006-04-24 13:22

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 Birkinshaw
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-04-24 15:24
(Received via mailing list)
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 Birkinshaw wrote:
>########################################################
>end
>
>http://lists.rubyonrails.org/mailman/listinfo/rails
_Kevin
7bbc5c78011f20a36ee0cffa15b575ef?d=identicon&s=25 Chris Birkinshaw (Guest)
on 2006-04-24 18:48
Kevin Olbrich 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 Birkinshaw wrote:
>>########################################################
>>end
>>
>>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
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2006-04-25 02:35
(Received via mailing list)
Chris Birkinshaw 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.
7bbc5c78011f20a36ee0cffa15b575ef?d=identicon&s=25 Chris Birkinshaw (Guest)
on 2006-04-25 15:08
Mark Reginald James 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
This topic is locked and can not be replied to.