I was a little confused about scope at work, so I will summarize it here as a memorandum. By the way, the person I am writing is a person who has been an engineer for one month. If you have any mistakes, please comment.
I want to pass variables for each to scope.
Office.rb
scope :search, lambda { |keyword|
where('name LIKE (?) OR
address LIKE (?) OR
near_station LIKE (?) OR
introduction LIKE (?) OR
company LIKE (?)',
"%#{keyword}%",
"%#{keyword}%",
"%#{keyword}%",
"%#{keyword}%",
"%#{keyword}%")
}
offices_controller.rb
keywords = params[:keyword].split(/[[:blank:]]+/).select(&:present?)
offices_list = []
keywords.each do |keyword|
offices_list += Office.search(keyword)
The code is only partially excerpted, but I was able to implement it this way. In the end, it became the above code, but the following code also worked normally.
Office.rb
scope :search, keyword { where('name LIKE (?) OR
address LIKE (?) OR
near_station LIKE (?) OR
introduction LIKE (?) OR
company LIKE (?)',
"%#{keyword}%",
"%#{keyword}%",
"%#{keyword}%",
"%#{keyword}%",
"%#{keyword}%")
}