app/models/category.rb
# Calculates post count in this category.
# Raw sql is used to leverage all load to db and max performance.
Calculates post count in this category.
Raw sql is used to leverage all load to db and max performance.
Calculates post count in this category . Raw sql is used to leverage all load to db and max performance .
def posts_count
    topic_ids = "SELECT id FROM topics
                 WHERE category_id = #{id}"
    sql_query = "SELECT COUNT (*) FROM posts WHERE topic_id IN (#{topic_ids})"
    records_array = ActiveRecord::Base.connection.execute(sql_query)
    records_array[0].values.first
  end
def posts_count topic_ids = "SELECT id FROM topics
                 WHERE category_id = #{id}" sql_query = "SELECT COUNT (*) FROM posts WHERE topic_id IN (#{topic_ids})" records_array = ActiveRecord :: Base . connection . execute ( sql_query ) records_array [ 0 ] . values . first end
Calculates post count in this category.
Calculates post count in this category .
# Selects ids of topics belonging to this category. # Black, dark magic but works with SQLite and PG.