There are many questions to help converting specific SQL query to ActiveRecord query.
Are there some assistance/guidance [online] tools to make the conversion automatica
here is the tool you are looking: http://www.scuttle.io/