Kai
Kai

Reputation: 23

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?

Upvotes: 1

Views: 366

Answers (1)

Nick Colgan
Nick Colgan

Reputation: 5508

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.

Upvotes: 1

Related Questions