Hi am combining a simple search facility with will_paginate pagination, based on the respective and excellent RailsCasts tutorials.
The code (in the model) is shown below,
def search(search, page)
# See RailsCasts #37 Simple Search Form & #51 will_paginate
if search
@matches = SalesActivity.where('salespeople.name LIKE ? OR products.name LIKE ?',
"%#{search}%", "%#{search}%")
@matches.paginate :per_page => 30,
:page => page,
:order => 'created_at DESC' # <--- causes exception
else
paginate :include => [:salesperson, :product],
:order => 'created_at DESC', # works
:per_page => 30,
:page => page
end
end
The ordering specified works fine in the paginate request on the path without the search.
However, on the path that use the search result (@matches), I get the following error,
SQLite3::SQLException: ambiguous column name: created_at: SELECT ... ORDER BY created_at DESC LIMIT 30 OFFSET
If I remove the order parameter, it works fine.
I’d appreciate suggestions for how to fix this. Thanks.
The fix here was to add ‘all’ to the end of the ‘where’ request, to initiate the actually query to load the data, as per Active Record Query protocol.