2017-04-24 123 views
0

我在我的视图中显示查询的结果,此查询使用两个不同的表和数据搜索我使用ransack gem执行搜索,但它显示我在搜索中出现以下错误形式:使用搜索宝石搜索

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

字段“PRODUCTO”我从所谓的“PRODUCTOS”另一个表中获取它,它是查询的结果,那只能说明我与另外一个,我等领域的错误在查询中,而不是与我所调用的方法相同,即“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 %> 

回答

0

首先,按照惯例,尝试不使用大写字母来命名列。

我们需要表格和模型的结构来给出更好的答案,所以请尝试将它们添加到问题中。

不过,我会尽量给你一些情况下,这样你就可以解决这个问题

如果您正在使用的has_many关系尝试,你会先打电话复数这种关系,然后该模型的列,例如,如果用户有很多的项目,我们希望通过这个项目的名称进行搜索,它会是这样

<%= f.search_field :projects_name_cont %> 

如果用户属于一个机构,我们将与奇异,然后到列型号如下

<%= f.search_field :institution_name_cont %> 

,如果我们在同一个用户模型一列尝试,那么这将是直接

<%= f.search_field :name_cont %> 

它会直接进入到用户模型中的名称列。希望这可以帮助你理解

+0

谢谢你的回应,非常感谢你的例子,我理解你的观点,但如果我想从查询中获取数据,请选择字段被重命名为“AS” ?例如,从这部分:: select(“[rutas] .Ruta AS ruta,[clientes] .IdCli AS Cliente”) – luis31