如何编写条件语句find
或paginate
方法以允许用户同时搜索Project
和Project Category
命名?
现在我的代码看起来像这样
@projects = Project.paginate :per_page => 20, :page => params[:page], :conditions => ['name LIKE ?', "%#{params[:search]}%"]
所以我也Category
有name
场地表.如何name
在这个搜索查询中将这两个组合在一起?
假设你的Project
模型has_many :categories
:
@projects = Project.paginate :per_page => 20, :page => params[:page], :joins => :categories, :conditions => ['projects.name LIKE ? OR categories.name LIKE ?', "%#{params[:search]}%", "%#{params[:search]}%"], :order => 'projects.id DESC'
:order
如果/需要,请更改上述内容.