简体   繁体   English

如何在Rails中编写以下查询?

[英]How can I write the following query in Rails?

I need to write a query that only counts individuals that have not attended an event previously. 我需要编写一个查询,该查询仅统计以前未参加活动的个人。

Here are the models 这是模型

class Individual < ActiveRecord::Base
  has_many :attendances, dependent: :destroy
  has_many :events, through: :attendances
end

class Attendance < ActiveRecord::Base
  belongs_to :event
  belongs_to :individual
end 

class Event < ActiveRecord::Base
  has_many :attendances, dependent: :destroy
  has_many :individuals, through: :attendances
end

In my view I have 2 different queries. 我认为我有2个不同的查询。 Each query measures event attendance during each fiscal year. 每个查询都会评估每个会计年度的事件出席率。

Query for FY 2015: 2015财年查询:

  <%= Event.published.joins( :attendances => :individual ).where( 
  :events => {:fiscal_session => "2014-10-01".."2015-09-30"}
  ).distinct.count(:individual_id) %>

Query for FY 2016: 查询2016财年:

  <%= Event.published.joins( :attendances => :individual ).where( 
  :events => { :fiscal_session => "2015-10-01".."2016-09-30"}
  ).distinct.count(:individual_id) %>

The problem is that the query for FY 2016 does not ignore individuals that attended an event in FY 2015. 问题在于,对于2016财年的查询不会忽略参加2015财年活动的个人。

I am using postgresql, and I have tried to write scopes and helper methods that would filter out individuals that attended in FY 2015. But I just haven't been able to figure it out. 我使用的是postgresql,我尝试编写范围和帮助程序方法,以过滤掉2015财年参加的个人。但是我只是无法弄清楚。

For example, here is FY 2015: 例如,这是2015财年:

<%= Individual.joins(attendances: :event).where( 
:events => {:fiscal_session => "2014-10-01".."2015-09-30"}
).group('individuals.id').having('count(attendances.id) > 0').count %>

The result is: 结果是:

{2787=>1, 3469=>1} 

And this is for FY 2016 这是2016财年

<%= Individual.joins(attendances: :event).where( 
:events => {:fiscal_session => "2015-10-01".."2016-09-30"}
).group('individuals.id').having('count(attendances.id) > 0').count %>

The result is this: 结果是这样的:

{2905=>1, 3444=>1, 2787=>1, 2790=>1, 2858=>1} 

As you can see, individual_id 2787 is counted twice. 如您所见, individual_id 2787被计数两次。 I do not want to count 2787 in the query for FY 2016. 我不想在2016财年的查询中计入2787

What is the proper way to achieve the desired count with Rails and ActiveRecord? 用Rails和ActiveRecord实现所需计数的正确方法是什么?

I think the best way for you to accomplish what you want to do is with two queries. 我认为完成您想要做的事情的最佳方法是使用两个查询。

ignore_individuals = Event
.published
.joins(attendances: :individual)
.where( 
  'events.fiscal_session <= ?', '2015-09-30'
)
.pluck(:individual_id)

Event
.published
.joins(attendances: :individual)
.where.not(individual_id: ignore_individuals)
.where( 
  events: { fiscal_session: "2015-10-01".."2016-09-30" }
).distinct.count(:individual_id)

I think you are looking for: Individual.includes(:events).where( events: { id: nil } ).count 我认为您正在寻找: Individual.includes(:events).where( events: { id: nil } ).count

These are the people who never attended any events. 这些人从未参加过任何活动。

不太乐观,但是如果您正在寻找参加2016年某项活动的个人人数,并且您的餐桌上有时间戳记,我相信您可以做到

Individual.joins(:attendances).where("attendances.created_at > ?", "01-01-2016").count

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM