'Avoiding SQL injection with a range in the HAVING Clause

How can I properly avoid SQL injection while taking advantage of Rails goodness. For example, this code:

Account.group("accounts.id").having("MIN(created_at)": 1.month.ago.all_month).first

Raises this exception:

Mysql2::Error: Unknown column 'accounts.MIN(created_at)' in 'having clause'

I could do it like this, but then I lose the beauty of the range:

Account.group("accounts.id").having("MIN(created_at) BETWEEN ? AND ?", 1.month.ago.beginning_of_month, 1.month.ago.end_of_month])


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source