I need help cleaning up this piece of code.if is_complete == true  #If there are time stamps get between time stamps  if start_date && end_date    tasks = Task.find(:all, :include => [:task_observer, :job, :contact], :conditions => [contact_id = ? and completed_date IS NOT NULL and DATE(completed_date) between ? and ?, contact_id, start_date, end_date])  else    tasks = Task.find(:all, :include => [:task_observer, :job, :contact], :conditions => [contact_id = ? and completed_date IS NOT NULL, contact_id])  endelse  #If there are time stamps get between time stamps  if start_date && end_date    tasks = Task.find(:all, :include => [:task_observer, :job, :contact], :conditions => [contact_id = ? and completed_date IS NULL and DATE(completed_date) between ? and ?, contact_id, start_date, end_date])  else    tasks = Task.find(:all, :include => [:task_observer, :job, :contact], :conditions => [contact_id = ? and completed_date IS NULL, contact_id])  endend
Task observer implementation
ruby;datetime;ruby on rails;active record
you can replace if is_complete == true with if is_completethe only thing that changes is :conditions, tasks = ... should not be inside the if/elsethe 3rd case is invalid: completed_date can't be NULL and within a certain range at the same timeuse one conditional for the NOT in completed_date IS (NOT) NULL and one for the and DATE ..., and concatenate the sql string then. Then the nested if/else is gone.the extra query arguments start_date and end_date depend only on start_date && end_date, so the distinctions should not be in the is_completed conditional.