thoughtbot / active-record-recipes

A repository of common (and not so common) active record queries.
54 stars 3 forks source link

Investigate casting duration to iso8601 #43

Closed stevepolitodesign closed 2 years ago

stevepolitodesign commented 2 years ago

We need to call iso8601 on any duration passed to a scope in order for the query to execute correctly. This is odd because Rails should know how to cast this value by default.

scope :quick, -> {
  joins(:steps).group(:id).having("SUM(duration) <= ?", 15.minutes)
}

Recipe.quick
# => operator does not exist: interval <= integer
stevepolitodesign commented 2 years ago

After further research, I determined that this is the expected behavior. Using a hash condition solves for this otherwise.