我需要对记录进行排序,我在单个表中使用单个数据的ransack gem,但是当我使用数据库中涉及多个表的查询时,我无法对其他表进行排序。
这是我的疑问:
def self.busqueda_rep_envases(params)
query = select("DevEnvases.ID, DevEnvases.CodCli AS Clave,C.Nombre as Responsable, C.NombreCorto as Nombre_Comercial,DevEnvases.Articulo,P.Producto,P1.Producto Envase,SUM(DevEnvases.Cantidad) Entregados,SUM(DevEnvases.Devuelto) Devueltos,SUM(DevEnvases.Cantidad)-SUM(DevEnvases.Devuelto) Saldo ")
.distinct
.joins('INNER JOIN Clientes C ON DevEnvases.CodCli=C.IdCli Inner Join Productos P ON DevEnvases.Articulo=P.Clave Inner Join Productos P1 ON DevEnvases.Envase=P1.Clave')
.where("(DevEnvases.Tipo = :Tipo_Env or :Tipo_Env = '') AND (DevEnvases.RutaId = :rutaId or :rutaId = '') AND (DevEnvases.DiaO = :diaO or :diaO = '') AND (DevEnvases.IdEmpresa = :idempresa)",{Tipo_Env: params[:Tipo_Env], rutaId: params[:search], diaO: params[:diaO], idempresa: params[:search6]})
.group('DevEnvases.ID, DevEnvases.CodCli,C.Nombre,DevEnvases.Articulo,DevEnvases.Tipo,P.Producto,P1.Producto,C.NombreCorto')
query
end
这是我在控制器中的方法:
def envases
@search = Devenvase.busqueda_rep_envases(params).search(search_params)
# make name the default sort column
@search.sorts = 'Clave' if @search.sorts.empty?
@envases = @search.result().page(params[:envases]).per(15)
end
这是我的搜索表单:
<%= search_form_for @search, :remote=>"true", url: reportes_ruta_envases_path, :method => :get do |f| %>
<tr>
<th class="component_name_header_col"><%= sort_link @search, :Clave, "Clave","devenvase", {}, { :remote => true, :method => :get } %></th>
<th class="component_name_header_col"><%= sort_link @search, :Responsable, "Responsable","devenvase", {}, { :remote => true, :method => :get } %></th>
<th class="component_name_header_col"><%= sort_link @search, :Nombre_Comercial, "Nombre_Comercial","devenvase", {}, { :remote => true, :method => :get } %></th>
<th class="component_name_header_col"><%= sort_link @search, :Articulo, "Articulo","devenvase", {}, { :remote => true, :method => :get } %></th>
<th class="component_name_header_col"><%= sort_link @search, :Producto, "Producto","devenvase", {}, { :remote => true, :method => :get } %></th>
<th class="component_name_header_col"><%= sort_link @search, :Envase, "Envase","devenvase", {}, { :remote => true, :method => :get } %></th>
<th class="component_name_header_col"><%= sort_link @search, :Entregados, "Entregados","devenvase", {}, { :remote => true, :method => :get } %></th>
<th class="component_name_header_col"><%= sort_link @search, :Devueltos, "Devueltos","devenvase", {}, { :remote => true, :method => :get } %></th>
<th class="component_name_header_col"><%= sort_link @search, :Saldo, "Saldo","devenvase", {}, { :remote => true, :method => :get } %></th>
</tr>
<% end %>
有没有办法通过查询进行搜索?还是可以做的其他一些宝石或方法?
答案 0 :(得分:0)
假设您的代码中已定义了关联(belongs_to,has_many等), 然后
将您的联接更改为只有表名:
joins(:Clientes, :Productos, :Productos1)....
将排序参数更改为此
<%= sort_link @search, :Clientes_Nombre, "Responsable" ....