Get count from another table

goterpsgo

I have a many-to-many, HMT model setup and I want to add a count value to return as part of my tab_community#index method. The models for the table of interest and the join table are as follows:

class TabCommunity < ApplicationRecord
  belongs_to :ref_community_type

  has_many :tab_client_project_communities
  has_many :tab_projects, through: :tab_client_project_communities

  has_many :tab_community_accounts
  has_many :tab_accounts, through: :tab_community_accounts

  has_many :tab_client_project_communities
  has_many :ref_platforms, through: :tab_client_project_communities
end

class TabCommunityAccount < ApplicationRecord
  belongs_to :tab_community
  belongs_to :tab_account
end

The #index method currently looks like this:

_tab_community_ids = params[:tab_community_ids].split(',')
@tab_communities = TabCommunity.where(id: _tab_community_ids).includes(:ref_platforms).all.order(:updated_at).reverse_order

This query is what I want to replicate in ActiveRecord:

select (select count(*) from tab_community_accounts where tab_community_id = c.id) as cnt, c.* from tab_communities c

The results I want are below:

7318    149 sports_writers  7   2017-12-17 15:45:36.946965  2017-12-17 15:45:36.946965
0   172 random_admin    8   2018-04-16 19:21:21.844041  2018-04-16 19:21:21.844041
2731    173 random_aacc 7   2018-04-16 19:22:35.074461  2018-04-16 19:22:35.074461

(The 1st column is count(*) from tab_community_accounts, the rest is from tab_communities.)

From what I've seen so far I should use either .select() or .pluck() but neither one works for me. I tried this out:

TabCommunity.pluck("distinct tab_community_accounts.tab_account_id as cnt").where(id: _tab_community_ids).includes(:ref_platforms).all.order(:updated_at).reverse_order

Is this close to what I need or am I completely off?

max

What you want is something like:

@tab_communities = TabCommunity
  .where(id: _tab_community_ids)
  .select('tab_communities.*, count(tab_community_accounts.id) AS cnt')
  .left_outer_joins(:tab_community_accounts)
  .includes(:ref_platforms) # consider if you actually need this
  .group(:id)
  .order(updated_at: :desc) # use an explicit order instead!

  TabCommunity Load (1.1ms)  SELECT tab_communities.*, count(tab_community_accounts.id) AS cnt FROM "tab_communities" LEFT OUTER JOIN "tab_community_accounts" ON "tab_community_accounts"."tab_community_id" = "tab_communities"."id" WHERE "tab_communities"."id" = 1 GROUP BY "tab_communities"."id" ORDER BY "tab_communities"."updated_at" DESC
=> #<ActiveRecord::Relation [#<TabCommunity id: 1, created_at: "2018-05-07 21:13:24", updated_at: "2018-05-07 21:13:24">]>

.select just alters the SELECT portion of the query. The result returned is still an ActiveRecord::Relation containing model instances.

ActiveRecord will automatically create an attribute for cnt:

irb(main):047:0> @tab_communities.map(&:cnt)
=> [1]

.pluck on the other hand just pulls the column values and returns an array or array of arrays if the query contains multiple columns.

@tab_communities = TabCommunity
  .where(id: _tab_community_ids)
  .left_outer_joins(:tab_community_accounts)
  .includes(:ref_platforms) # consider if you actually need this
  .group(:id)
  .order(updated_at: :desc)
  .pluck('tab_communities.id, count(tab_community_accounts.id) AS cnt')

 (1.0ms)  SELECT tab_communities.id, count(tab_community_accounts.id) AS cnt FROM "tab_communities" LEFT OUTER JOIN "tab_community_accounts" ON "tab_community_accounts"."tab_community_id" = "tab_communities"."id" WHERE "tab_communities"."id" = 1 GROUP BY "tab_communities"."id" ORDER BY "tab_communities"."updated_at" DESC
=> [[1, 1]]

Using .* with pluck is not a good idea since you don't know what order the attributes have in the resulting array.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Join a columns count from another table

From Dev

SQL query with count() from another table

From Dev

Adding a count from another table to existing query

From Dev

Count from another table in FROM subquery

From Dev

mysql - count from different table supplemented with another count from subresult

From Dev

SQL COUNT to count all the numbers from another table

From Dev

MySQL count of count, use result from one table with another

From Dev

Trying to use subquery to get row count from another table

From Dev

How to get count from another table (zero when no data) using single query in mysql?

From Dev

How to get count of two fields from two different table with grouping a field from another table in mysql

From Dev

Count Row from another table

From Dev

How use count for value from another table

From Dev

SQLite select and count from another table with like

From Dev

Selecting records from a table and then selecting a count of records from another table

From Dev

Get data from one table by reference of another table's column's count(*)

From Dev

MySQL - Join & Count rows from another table

From Dev

How to count data from another table

From Dev

get th count from table and details from another table

From Dev

how to get count of another table

From Dev

How to get count from another table (zero when no data) using single query in mysql?

From Dev

sql - show count of field from another table

From Dev

Select from one table and count from another

From Dev

How to get count of two fields from two different table with grouping a field from another table in mysql

From Dev

Select * as well as count/sum from another table

From Dev

How to count rows from another table to affect another table

From Dev

Display the details from one table and count from another table

From Dev

SQL COUNT Rows from another table

From Dev

Filter by count from another table

From Dev

MYSQL select from table and count from another

Related Related

  1. 1

    Join a columns count from another table

  2. 2

    SQL query with count() from another table

  3. 3

    Adding a count from another table to existing query

  4. 4

    Count from another table in FROM subquery

  5. 5

    mysql - count from different table supplemented with another count from subresult

  6. 6

    SQL COUNT to count all the numbers from another table

  7. 7

    MySQL count of count, use result from one table with another

  8. 8

    Trying to use subquery to get row count from another table

  9. 9

    How to get count from another table (zero when no data) using single query in mysql?

  10. 10

    How to get count of two fields from two different table with grouping a field from another table in mysql

  11. 11

    Count Row from another table

  12. 12

    How use count for value from another table

  13. 13

    SQLite select and count from another table with like

  14. 14

    Selecting records from a table and then selecting a count of records from another table

  15. 15

    Get data from one table by reference of another table's column's count(*)

  16. 16

    MySQL - Join & Count rows from another table

  17. 17

    How to count data from another table

  18. 18

    get th count from table and details from another table

  19. 19

    how to get count of another table

  20. 20

    How to get count from another table (zero when no data) using single query in mysql?

  21. 21

    sql - show count of field from another table

  22. 22

    Select from one table and count from another

  23. 23

    How to get count of two fields from two different table with grouping a field from another table in mysql

  24. 24

    Select * as well as count/sum from another table

  25. 25

    How to count rows from another table to affect another table

  26. 26

    Display the details from one table and count from another table

  27. 27

    SQL COUNT Rows from another table

  28. 28

    Filter by count from another table

  29. 29

    MYSQL select from table and count from another

HotTag

Archive