I need to have a form that autocompletes, and I'm using Ransack
with jQuery
, the subject is with Ransack
.
THE problem: I need to have a form that in a single field look for two different models or maybe up to 3.
I have the models:
class Proyecto <ActiveRecord::Base
has_many :actividades
end
class Actividad <ActiveRecord::base
belongs_to :proyecto
def self.search2(dato)
where('LOWER(numero) LIKE :dato', dato: "%#{dato.downcase}%")
end
end
On the controller:
def search
@buscar = Proyecto.ransack(params[:q])
@actividades = @buscar.result.joins(:actividades).select('actividades.numero as actividades_numero, proyectos.nombre as proyecto_nombre')
respond_to do |format|
format.html { @buscar }
format.json { @actividades = Actividad.search2(params[:term])}
end
end
In the view:
<%=search_form_for(@buscar, url: "/administrador/atividades/search", class: "form-inline", role: "form") do |f| %>
<%=f.search_field :actividades_nombre_cont, class: "form-control", id: "items-search-txt", placeholder: "Coloque el número de la cohorte", style:"width:330px" %>
The issue is that it does not work, put what you put in f.search_field
, I need to search by project name or by activity number.
I understand that I must place all the fields for which to search in the way campo1 or campo2 or campo3 cont
in f.search_field
, but how do I call the fields?