I have two small problems with a query
1) I am conducting a search of the clients related to the logs, this query should bring me those bitacoratime logs where the code is equal to the id of the clients, the problem is that the clients should not repeat themselves, if there is 3 logs with the same client I want to only take the first record, try to do it with .distinct but it does not work that way.
2) The next error is that in the comparison between dates because it is a datetime if I search between the date 2014-06-28 and the 2014-06-30 and there is a record of the following form 2014-06- 30 13:06:20 will not take it into account in the consultation because it is in a higher range due to the time it has. How could I fix it?
def self.total_clientes(params)
query = joins("left outer join clientes on bitacoratiempos.Codigo=clientes.IdCli")
.where("(bitacoratiempos.RutaId = :rutaId or :rutaId = '') AND (bitacoratiempos.IdEmpresa = :idempresa)",{rutaId: params[:search], idempresa: params[:search0]})
.distinct
query = query.where('bitacoratiempos.HI between ? AND ?', (params[:search1].to_date).strftime('%Y-%m-%d'), (params[:search2].to_date).strftime('%Y-%m-%d')) if params[:search1].present? and params[:search2].present?
query
end