I'm trying to search multiple columns in my model, by category. I was trying to use Model.scoped, but that was deprecated in Rails 4. Model.all seems to return all models, regardless of my other scopes. Is there an alternative to search the columns on my model and does my code look right? Would prefer to have one field to search multiple columns based on category. The search gems seem to be a bit overkill for what I'm trying to do. Below are my files.
vendor.rb (Edit from depa's comments)
belongs_to :category
def self.search(name, phone, email)
scope = Vendor.all #still returns all in my model regardless of what I enter in search form.
scope = scope.where(name: name) if name
scope = scope.where(phone: phone) if phone
scope = scope.where(email: email) if email
scope
end
category.rb
has_many :vendors
vendors_controller.rb
def index
@vendors = Vendor.order('created_at DESC')
end
def search
@vendors = Vendor.search(params[:name], params[:phone], params[:email])
end
vendors/index.html.erb
<%= form_tag search_vendors_path, method: :get do |f| %>
<%= text_field_tag :search, params[:search], placeholder: "Search Vendors" %>
<%= select_tag 'category', options_for_select(Category.all.map{|el| [el.name, el.id]}) %>
<%= submit_tag 'Search' %>
<% end %>