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?
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:
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.
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.