How do you create a dynamic where clause for a custom SQL query in rails?
We need to do a count with a double group by, which to my knowledge is not supported by ActiveRecord in Rails 2.3, so we're doing a custom SQL query, something like:
ActiveRecord::Base.connection.execute("select COUNT(*), author_id, call_type_id from signup_calls group by author_id, call_type_id")
However, we also need to filter the signup calls being counted by three user selected filters, which need to be included in the where clause.
Is there someway I can use the ActiveRecord helpers to help generate the where clause. If not, what's the best way to go about it?
You can do
SignupCall.select('count(*) as signup_count, author_id, call_type_id').group('author_id, call_type_id')
Then just chain where clauses afterwards.