ruby - Converting a Postgres query to Rails ActiveRecord? -
is possible write following query in rail's activerecord format? i've tried billion different ways arel can't same results.
select topic_id, count(*) total questions_topics question_id in ( select id questions user_id = 1000 union select questions.id questions join answers on (questions.id = answers.question_id) answers.user_id = 1000 ) group topic_id order total desc;
well, should work. not same thing should give same results.
questiontopic.where( "question_id in (?) or question_id in (?)", question.where(user_id: 1000).select(:id), answer.where(user_id: 1000).select(:question_id) ).group('topic_id') .order('total desc') .select('topic_id, count(*) total')
Comments
Post a Comment