Rika
Rika

Reputation: 171

Searching for two arguments Ruby on Rails

I am working on a search with two text fields. It searches for two things (right now location and description) and only shows the entries (listings) that match both of them. I would like the second text field to also search for title, so it should look for description and title. How would that work?

This is what I have right now

listing.rb

def self.locsearch(search_location, search_description)
  return scoped unless search_location.present? || search_description.present?
  where(['location LIKE? AND description LIKE?', "%#{search_location}%", "%#{search_description}%"])
end

home.html.erb

<%= form_tag findjobs_path, :controller => 'listings', :action => 'locsearch', method: :get do %>
  <%= text_field_tag :location, params[:location] %>
  <%= text_field_tag :descripiton, params[:descripiton] %>
  <%= submit_tag "Search", name: nil %>
  <% end %

listings_controller.rb

def index
@listings = @listings.locsearch(params[:location], params[:description])
end

Also, my locsearch method right now uses the or || condition. How would I implement the "and" condition? (If I change the || to && I get the error" undefined method or variable scroped)

Upvotes: 0

Views: 75

Answers (1)

Richa Sinha
Richa Sinha

Reputation: 1456

Are you sure you want to use unless

unless is used only if you want to execute a specified code if the condition(s) is false.

And scoped is used along with a model.

Model.scoped

You can refer the Apidock or Github

You can write return scoped only if u have defined scoped as a local variable or a method.

You can also see scope for databases

Upvotes: 2

Related Questions