I'm using a query to search for records, I have two tables, one is "sale" and the other is "detallevet" (this refers to the details of the sale) by this query I proceed to do the search of sales in their corresponding model
def self.busqueda_general(params)
query = joins("left outer join detallevet on venta.Documento=detallevet.Docto and venta.RutaId=detallevet.RutaId")
.where("(venta.RutaId = :rutaId or :rutaId = '') AND (detallevet.Articulo = :articulo or :articulo = '') AND (venta.CodCliente = :codcliente or :codcliente = '')",{rutaId: params[:search], articulo: params[:search3], codcliente: params[:search2]})
.distinct
end
then I call this method in the controller in a method called search_sales
def busqueda_ventas
@detallevet = Detalleve.all
@ventas = Vent.busqueda_general(params)
respond_to do |format|
format.js
end
end
sales are related to retail sales but not by a field sale_id that identifies them, but by means of two fields, one called document and another RutaId in both tables, both combinations must be equal to relate. in my view I am ordering all my sales with their details respectively, to do this in the controller method look for all the details in this way @detallevet = Detalleve.all and in the view I did a render as follows
<%= render '@detallevet.where(:Docto => vent.Documento, :RutaId => vent.RutaId) %>
as for the data shown:
<td><%=vent.IVA%></td>
<td><%[email protected](:Docto => vent.Documento, :RutaId => vent.RutaId ).sum(:DescMon)%></td>
<td><%=vent.TOTAL%></td>
<td><%[email protected](:Docto => vent.Documento, :RutaId => vent.RutaId).count%></td>
<td><%[email protected](:Docto => vent.Documento, :RutaId => vent.RutaId).sum(:Pza)%></td>
but it seems to me that I am making use of bad practices placing that type of consultations in the view, and if so, how could I improve it?
UPDATED CODE:
has_many :detallevet, -> (vent) { where(Docto: vent.Documento, RutaId: vent.RutaId) },
class_name: 'Detalleve'