I am working with the brakeman gem (which identifies possible security issues in Ruby on Rails code). We have a scope which uses joins, group and select and I need to update the select part of the query. This scope looks something like the below. I am putting content of the scope here on product model.
Product.joins('LEFT JOIN orders ON orders.product_id = products.id')
.group('products.id')
.select(
"SUM(CASE WHEN orders.order_at BETWEEN '#{start_date}' AND '#{end_date}'" \
" THEN orders.qty ELSE 0 END) as qty, products.*"
)
Now If i try to change query like this
Product.joins('LEFT JOIN orders ON orders.product_id = products.id')
.group('products.id')
.select(
"SUM(CASE WHEN orders.order_at BETWEEN ? AND ?" \
" THEN orders.qty ELSE 0 END) as qty, products.*",
'#{start_date}',
'#{end_date}'
)
It gives me syntax error where I have ? I have also tried some other ways with group and having but it didn't worked for me. I am using postgres sql with rails 4.1.8
Is their any way I can achieve this? Thanks in advance.
This is the error
PG::SyntaxError: ERROR: syntax error at or near "?"
LINE 1: SELECT SUM(CASE WHEN orders.order_at BETWEEN ? AND ? AND ord