简体   繁体   中英

Search with ransack gem

I am showing the result of a query in my view, this query uses two different tables and for the data search I am using the ransack gem to perform the searches but it shows me the following error in the search form:

ActionView::Template::Error (undefined method `Producto_cont' for Ransack::Search<class: Stoc, base: Grouping <combinator: and>>:Ransack::Search):

The field "Producto" I am getting it from another table called "productos" and it is the result of the query, it only shows me the error with the fields of the other one that I am relating in the query and not with the one that I am calling from the method that It's "Stoc"

this is my method controller:

@search = Stoc.productos_de_un_stock(params).search(search_params)
@search.sorts = 'Stock desc' if @search.sorts.empty?
@stock = @search.result().page(params[:stock]).per(15)

this is my query:

  def self.productos_de_un_stock(params)
      query = select("[stock].IdStock, [stock].Stock, [productos].Clave AS Clave_producto, [productos].Producto, [productosxpzas].PzaXCja AS PzaXCja")
              .joins('inner join productos ON stock.Articulo = productos.Clave inner join productosxpzas ON productos.Clave = productosxpzas.Producto')
              .where('stock.Ruta = ? AND stock.IdEmpresa = ?', (params[:search]), (params[:search6]))

      query
    end

this is my search form:

  <%= search_form_for @search, :remote=>"true", url: busqueda_stock_path, :method => :get do |f| %>
    <div class="rwd">
      <table class="table table-striped table-bordered table-condensed table-hover rwd_auto">
        <thead>
          <tr>
            <th class="component_name_header_col"><%= sort_link @search, :Articulo, "Clave","stoc", {}, { :remote => true, :method => :get } %></th>

            <th class="action_col"><%= t("basic.action") %></th>
          </tr>
          <tr>
            <th><%= f.text_field :Articulo_cont, class:"campo" %></th>
            <%= f.search_field :Producto_cont %>
              <input id="search" type="hidden" value="<%=params[:search] %>" name="search"/> 
            <input id="search6" type="hidden" value="<%=params[:search6] %>" name="search6"/>


            <th><%= f.submit "Buscar" %></th>
          </tr>

        </thead>
        <tbody>

        </tbody>
      </table>
    </div>
  <% end %>

First, by convention, try to name the column without uppercase.

and we need the structure of the tables and models to give better answer, so please try to add them to the question.

But I will try to give you some cases so you can solve the problem

if you are trying with a has_many relation, you will first call that relation in plural and then the column of that model, for example, if an user have many projects and we want to search by the name of the project, it will be like this

 <%= f.search_field :projects_name_cont %>

if the user belongs to an institution, we will go with singular and then to the column of the model, like this

<%= f.search_field :institution_name_cont %>

and if we try with a column in the same user model, then it will be directly

 <%= f.search_field :name_cont %>

it will go directly to the name column in the user model. hope that this helps you to understand

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM