0

目前附件是根据attachment_file_name,tags和搜索的employee_id,我也想根据员工姓名搜索,我该怎么做呢?必须使用两个模型。

  1. 员工- 有员工姓名
  2. 课程计划附件- 包含员工的 ID。

代码部分。

def search_ajax
   @attachments = LessonplanAttachment.find(:all,
    :conditions => ["attachment_file_name LIKE ? OR tags LIKE ? OR employee_id = ?",
      "#{params[:query]}%","#{params[:query]}%",params[:query]])
end
4

3 回答 3

0

尝试这个

LessonplanAttachment.joins("JOIN employee on employee_id").find(:all, :conditions =>
  ["attachment_file_name LIKE ? OR tags LIKE ? OR employee_id = ? OR employee.name = ?",
  "#{params[:query]}%","#{params[:query]}%",params[:query], params[:query]]
于 2013-07-23T10:22:29.403 回答
0

我猜您的 LessonplanAttachment 模型中有以下关联。

belongs_to :employee

现在您可以按照以下方式更改您的代码

@attachments = LessonplanAttachment.joins([:employee]).where("attachment_file_name LIKE ? OR tags LIKE ? OR employee_id = ? OR employees.name LIKE ?", "#{params[:query]}%","#{params[:query]}%",params[:query],"#{params[:query]}%")
于 2013-07-23T10:22:40.193 回答
0

belongs_to :employee如果您在类上有关联LessonplanAttachment,对于 rails 2,您可以将:joins属性添加到find. 像这样的东西应该工作:

@attachments = LessonplanAttachment.find(:all,
 :joins => :employee,
 :conditions => [
   "attachment_file_name LIKE ? OR tags LIKE ? OR 
    employee_id = ? OR employees.name LIKE ?",
   "#{params[:query]}%","#{params[:query]}%",params[:query],"#{params[:query]}%"])
于 2013-07-23T13:20:43.600 回答