rails 模型不适用于 distinct 语句

rails model did not work with distinct statement

这是我的问题:我正在根据 rails 5.1.6sqlite3 查询数据,我想 select raws 具有相同的事件名称 login同一天,区别于用户(用户可能在同一天多次登录系统)。下面是我的代码:

class Query < ApplicationRecord

  def self.event_finder(name, time, **options)

    opt = {
      country_code: "CA",
    }

    if options.any?
      opt = opt.merge(options)
    end

    self.where("event_time >= ?", time)
        .where("event_time < ?", time + 1.day)
        .where(opt)
        .distinct(:user_id)
  end

end

当我 运行 我的模型在 rails console 时,它 return,但没有 return 正确数据,结果在关键字 [=16= 中重复].那么如何使用 rails 模型删除重复项?

像下面这样尝试

self.where("event_time >= ?", time)
    .where("event_time < ?", time + 1.day)
    .where(opt)
    .select('distinct(user_id)')

或者你可以这样使用

self.where("event_time >= ?", time)
    .where("event_time < ?", time + 1.day)
    .where(opt)
    .distinct.pluck(:user_id)

这也有效

self.where("event_time >= ?", time)
    .where("event_time < ?", time + 1.day)
    .where(opt)
    .pluck("DISTINCT user_id")