In the search function of my application, where a user can input a search text and pick a category (subject) from a dropdown.
In the do_search action in my controller, I find the courses by doing Subject.find(params[:subject]).courses where params[:subject] is the s_id (subject ID) of each Subject ("1234-123", "4123-232", ...).
When running the server, I tried searching but no result is found. I also checked it in the rails console but 0 matches as well.
This is the code for the do_search action in my controller:
def do_search
if params[:subject].blank? && params[:search_text].blank?
@courses = Course.all
elsif !params[:subject].blank? && !params[:search_text].blank?
@courses = Subject.find(params[:subject]).courses.where("UPPER(name) LIKE ?", "%#{params[:search_text].upcase}%")
elsif !params[:subject].blank?
@courses = Subject.find(params[:subject]).courses
else
@courses = Course.where("UPPER(name) LIKE ?", "%#{params[:search_text].upcase}%")
end
return @courses
end
The schema: courses - subjects: many-to-many
subjects
- s_id (the Subject ID): string
- name: string
courses
- code: string
- name: string
contains
- course_id: integer (primary key created by ActiveRecord for each course)
- subject_id: integer (primary key created by ActiveRecord for each subject)