One of the most satisfying feelings as a software developer is when you come across an elegant solution to a complicated problem.

I was trying to distill down the statuses of the many side of a 1 to many relationship so that the 1 side indicated the overall status of not started, in progress and finished. I ended up boiling it all down to the following:

def current_status  
  not_started = [:requires_action, :not_started]
  finished = [:deleted, :expired, :finished]

  related_states = related_objects.pluck("status"){|s| s.to_sym }

  return :not_started if (related_states-not_started).empty?
  return :finished if (related_states-finished).empty?




First we define the states that constitute not started and finished as arrays then get the list of states of the related objects. Subtracting the arrays checks if all related states are not started or finished, if its anything else, its in progress.