0

Currently the attachments are searched based on attachment_file_name, tags and employee_id, I would like to search based on Employee name as well, how can I go with it? Two models must be used.

  1. Employee - which has name of employee
  2. LessonplanAttachment - contains the id of the employee.

The code part.

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
AnkitG
  • 6,438
  • 7
  • 44
  • 72
Nithin
  • 3,679
  • 3
  • 30
  • 55

3 Answers3

0

try this

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]]
Slicedpan
  • 4,995
  • 2
  • 18
  • 33
0

I guess you have the following association in your LessonplanAttachment model.

belongs_to :employee

Now you can change your code as per the following

@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]}%")
Bachan Smruty
  • 5,686
  • 1
  • 18
  • 23
0

Provided you have an association belongs_to :employee on your LessonplanAttachment class, for rails 2, you can add a :joins attribute to the options on find. Something like this should work:

@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]}%"])
Shadwell
  • 34,314
  • 14
  • 94
  • 99