Ransack and multiple models in Rails

1

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?

    
asked by josedes 28.11.2016 в 20:56
source

1 answer

0

This is what I understand you are trying: Search for projects, both for fields related to projects and for related table fields. Ransack gives you that option:

# Controlador
def search
  respond_to do |format|
    format.html do
      @buscar = Proyecto.includes(:actividades).ransack(params[:q])
      @proyectos = @buscar.result
    end
    format.json { @actividades =  Actividad.search2(params[:term])}
  end
end


# Vista.
<%=search_form_for(@buscar, url: "/administrador/atividades/search", class: "form-inline", role: "form") do |f| %>               
  <%=f.search_field :nombre_or_actividades_numero_cont, placeholder: "Busque por número de proyecto o número de actividad", class: "form-control" %>
  <%= f.submit %>
<% end %>

<% @proyectos.each do |proyecto| %>
...

Notes:

  • I could have added the includes afterwards but it should also work.
  • Ransack comes with helpers for the forms and allows us to do a search in multiple columns joining them with _or _ . Example if we have the fields name and last name and we want you to search in either of the two indistinctly we would use: name_or_surname_cont . ( _cont is contains )
  • It also allows us to search associations. In that case, he asks us to include the name of the association in the formation of the symbol. In the example above it is _cont_number activities assuming that the activity table has a column number for which you want to search. If instead you want to search by the ID it would be activities_id_cont .
  • As in the example I'm using contains ( _cont ) and not equal ( _eq ) if I make a search for activity 1, it will bring all those that contain the number 1: 1, 10, 11, 12, etc. In that case, better to use _eq
answered by 10.03.2017 в 19:21