Ruby on rails 从另一个表中获取计数
我有一个多对多HMT模型设置,我想添加一个计数值作为我的tab_communityindex方法的一部分返回。兴趣表和联接表的模型如下所示:Ruby on rails 从另一个表中获取计数,ruby-on-rails,postgresql,activerecord,Ruby On Rails,Postgresql,Activerecord,我有一个多对多HMT模型设置,我想添加一个计数值作为我的tab_communityindex方法的一部分返回。兴趣表和联接表的模型如下所示: class TabCommunity < ApplicationRecord belongs_to :ref_community_type has_many :tab_client_project_communities has_many :tab_projects, through: :tab_client_project_commu
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
我希望在ActiveRecord中复制此查询:
select (select count(*) from tab_community_accounts where tab_community_id = c.id) as cnt, c.* from tab_communities c
我想要的结果如下:
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
第一列是tab_社区账户中的count*,其余列是tab_社区账户中的count*
从目前为止我所看到的情况来看,我应该使用.select或.pulk,但两者都不适合我。我试过:
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
这是接近我需要的还是我完全不需要了?你想要的是:
@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!
.另一方面,如果查询包含多个列,则Pull只提取列值并返回一个或多个数组
@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')
将。*与Pulk一起使用不是一个好主意,因为您不知道属性在结果数组中的顺序。您想要的是:
@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!
.另一方面,如果查询包含多个列,则Pull只提取列值并返回一个或多个数组
@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')
使用.*with pulk不是一个好主意,因为您不知道属性在结果数组中的顺序。.left\u outer\u联接是在Rails 5中引入的。有关遗留应用程序的解决方法,请参见。Rails 5中引入了左外连接。有关遗留应用的解决方法,请参阅。
@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]]