ruby - Getting wrong number when using count - Rails -


i trying total number of questions based on specific categories. used below line of code returns wrong number , cannot understand why cannot access questions directly "where" query.

def questions_total     number = category.where("tag ?",'1-%').includes(:questions).count   end 

please guide me through mistakes thanks

use group by sql syntax:

question.group(:category_id)         .joins(:category).where('categories.tag ?', '1-%')         .count 

you can delete joins line if don't want apply filter based on category attribute.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -