status_trend.rb 795 B

123456789101112131415161718192021
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: status_trends
  5. #
  6. # id :bigint(8) not null, primary key
  7. # status_id :bigint(8) not null
  8. # account_id :bigint(8) not null
  9. # score :float default(0.0), not null
  10. # rank :integer default(0), not null
  11. # allowed :boolean default(FALSE), not null
  12. # language :string
  13. #
  14. class StatusTrend < ApplicationRecord
  15. belongs_to :status
  16. belongs_to :account
  17. scope :allowed, -> { joins('INNER JOIN (SELECT account_id, MAX(score) AS max_score FROM status_trends GROUP BY account_id) AS grouped_status_trends ON status_trends.account_id = grouped_status_trends.account_id AND status_trends.score = grouped_status_trends.max_score').where(allowed: true) }
  18. end