[英]Search with ransack gem
我在视图中显示查询的结果,该查询使用两个不同的表,并且对于数据搜索,我使用了ransack gem执行搜索,但在搜索表单中显示了以下错误:
ActionView::Template::Error (undefined method `Producto_cont' for Ransack::Search<class: Stoc, base: Grouping <combinator: and>>:Ransack::Search):
我从另一个称为“ productos”的表中获取“ Producto”字段,它是查询的结果,它只向我显示与我在查询中相关的另一个字段的错误,而不是与我相关的错误我从“ Stoc”方法中调用
这是我的方法控制器:
@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)
这是我的查询:
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
这是我的搜索表:
<%= 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 %>
首先,按照惯例,尝试命名不带大写字母的列。
并且我们需要表和模型的结构来提供更好的答案,因此请尝试将其添加到问题中。
但我会尝试给您一些案例,以便您解决问题
如果您尝试使用has_many关系,则将首先以复数形式调用该关系,然后再调用该模型的列,例如,如果用户有很多项目,而我们要按项目名称进行搜索,则就像这个
<%= f.search_field :projects_name_cont %>
如果用户属于一个机构,我们将使用单数形式,然后是模型的列,如下所示
<%= f.search_field :institution_name_cont %>
如果我们尝试使用同一用户模型中的列,那么它将直接
<%= f.search_field :name_cont %>
它将直接转到用户模型中的名称列。 希望这可以帮助您了解
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.