Rails 3 - Turning multiple counts into a single query - OrderedHash?

I don't think you can do this cleanly in one query. At least not without writing pure sql.

I don't think you can do this cleanly in one query. At least not without writing pure sql. But lets try and find a nice solution in ActiveRecord First of all, let's try to remove some of the sql UserAnswer.

Count(:conditions => 'u. Anonymous = 0 AND q. Id =?', question_id, :joins => 'JOIN answers a ON user_answers.

Answer_id = a. Id JOIN questions q ON q.Id = a. Question_id JOIN users you ON u.

Id = user_answers. User_id') can be rewritten UserAnswer. Joins(:user).

Where(:users => {:anonymous => false})\ . Joins(:answer => :question). Where(:questions => {:id => question_id})\ .

Count lets just save this scope as a magic private method magic_scope your current methods become def total_answers(question_id) magic_scope(question_id). Count end def load_stats_total(question_id) magic_scope(question_id). Count(:group => "answers.

Correct") end def load_stats_answers(question_id) magic_scope(question_id). Count(:group => "answers. Id") end notably, of course, the total_answers method can be derived from summing up the results from either of the load_stats_* methods.

If ActiveRecord was a bit more clever we could do def all_the_magic(question_id) magic_scope(question_id). Count(:group => "answers. Correct", "answers.

Id") end which would give us all the data we needed to do it in one query. But as far as I'm aware that's not currently possible. But I hope this gets you nearer.

Welp, I guess this is good enough for now. Thanks Matthew! – wulftone Jul 7 at 5:17.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions