Home > Back-end >  Can Rails search across multiple columns in one query?
Can Rails search across multiple columns in one query?

Time:03-30

I wrote a simple search function that just looks for the value in the "name" field. How can I rewrite it if I want to search more fields at the same time?

<%= form_tag search_tasks_path, :method=> 'get' do %>
        <p>
          <%= text_field_tag :name, params[:name] %>
          <%= submit_tag "Search" , :name=> nil %>
        </p>
<% end %>

Can Rails search find plural columns?

CodePudding user response:

I understand you want to search across multiple fields in your form. You need to add another field to the form by which you want to search, and you must process the result of the response in the controller action.

@tasks = Task.where('name=? OR some_another_field=?', params[:name], params[:some_another_fields])
  • Related