English 中文(简体)
B. 采用加入书计算社团的主动查询
原标题:ActiveRecord query for count of association using a join table

我会做一个活动,我会陷入一个相当牵涉的问题。

此处为参考模型:

class Event < ApplicationRecord
  has_one :party

  belongs_to :host, foreign_key: :host_id, class_name:  User 
  belongs_to :convention, optional: true
  has_many :join_requests
  
  ...
end

I m 通过核对相关缔约方的<代码>——有限,检查哪些活动是接受成员。

class Party < ApplicationRecord
  belongs_to :event
  belongs_to :host, foreign_key: :host_id, class_name:  User 

  has_many :user_parties
  has_many :members, through: :user_parties, source:  user 

  ...
end

<代码>Party有一个分类栏:member_limit——该栏只允许一组缔约方成员。

迄今为止,我有这样的活动余地:

scope :accepting_members, -> () do
    joins(party: :user_parties).group( parties.member_limit ).select( count(user_parties.user_id) < parties.member_limit )
  end

哪些工作做得太好。

我在正确轨道上发言吗?

我尝试使用COUNT(COUNT)的总合,但只回收了一批ger。

问题回答

我认为,你没有必要与你一道这样做。

由于关系如下:

  1. Event has_one Party
  2. Party has_many UserParty

你可以这样做。

# event.rb
scope :accepting_members, -> { party.user_parties.count < party.member_limit }

这将使养羊人返回。 如果成员限额没有达到,则会以其他不实方式返回。

如果你想要获得可喜的产出,那将变得更加复杂,那么你最初认为这样做有几条方法。

一种办法是为活动设立分局:

SELECT events.* 
FROM events
WHERE events.id IN (
  SELECT parties.events_id
  FROM parties
  OUTER LEFT JOIN user_parties ON parties.id = user_parties.party_id
  GROUP BY parties.event_id
  HAVING(
    COUNT(user_parties.user_id) < parties.member_limit
  )
)

在“积极记录”和“Arel”中撰写这篇论文,但与我一起:

subquery = Party.select(:events_id)
                .left_joins(:user_parties)
                .group(:event_id)
                .having(
                  # COUNT(user_parties.user_id) < parties.member_limit
                  UserParty.arel_table[:user_id].count.lt(
                    Party.arel_table[:member_limit]
                  )
                )
                .where(
                  # events.id = parties.event_id
                  Event.arel_table[:id].eq(Party.arel_table[:id])
                )
 
Event.where(
  id: subquery
)

这样做的其他方式是使用<代码>EXIST或横向加入。





相关问题
摘录数据

我如何将Excel板的数据输入我的Django应用? I m将PosgreSQL数据库作为数据库。

Postgres dump of only parts of tables for a dev snapshot

On production our database is a few hundred gigabytes in size. For development and testing, we need to create snapshots of this database that are functionally equivalent, but which are only 10 or 20 ...

How to join attributes in sql select statement?

I want to join few attributes in select statement as one for example select id, (name + + surname + + age) as info from users this doesn t work, how to do it? I m using postgreSQL.

What text encoding to use?

I need to setup my PostgreSQL DB s text encoding to handle non-American English characters that you d find showing up in languages such as German, Spanish, and French. What character encoding should ...

SQL LIKE condition to check for integer?

I am using a set of SQL LIKE conditions to go through the alphabet and list all items beginning with the appropriate letter, e.g. to get all books where the title starts with the letter "A": SELECT * ...

热门标签