您好,我将一个简单的搜索工具与 will_paginate 分页相结合,基于各自的优秀 RailsCasts 教程。
代码(在模型中)如下所示,
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
指定的排序在没有搜索的路径上的分页请求中工作正常。
但是,在使用搜索结果 (@matches) 的路径上,出现以下错误,
SQLite3::SQLException: ambiguous column name: created_at: SELECT ... ORDER BY created_at DESC LIMIT 30 OFFSET
如果我删除 order 参数,它工作正常。
我很感激有关如何解决此问题的建议。谢谢。