mardi 20 septembre 2016

if inside where query on rails

i have this query:

  def self.search(nombre,zona,tipoActividad,fechaInicio,fechaFin,nombreProfesor)

    where("nombre) iLIKE ? or contenido iLIKE ? or descripcion iLIKE ? ", "%#{nombre}%", "%#{nombre}%","%#{nombre}%")
      .where("lugar iLIKE ?", "%#{zona}%")
      .where("tipo_actividad iLIKE ?", "%#{tipoActividad}%")
      .where(['fecha_inicio >= ?', fechaInicio]) ***if !fechaInicio.blank?***
      .where(['fecha_fin <= ?', fechaFin]) ***if !fechaFin.blank?***
      .joins(:user).where("users.lastname iLIKE ?", "%#{nombreProfesor}%")
  end

I have the problem when i have to filter by fecha_fin and fecha_inicio. i need to filter by them only if the parameters are present. How can I re-change the query yo be ok?

Thanks.

Aucun commentaire:

Enregistrer un commentaire