:viewable_imp, lambda { where(action: 'imp') } scope :today, lambda { where(dt: Time.zone.today) } end # 参照 PvLog.vieable_imp.today.count class PvLogAggregator def initialize(date) @date = date @bigquery = Google::Cloud::Bigquery.new end def aggregate @bigquery.query(query) end def query <<-SQL.strip_heredoc select count(1) as pv from pv_logs where dt = '#{@date}' and action = 'imp' SQL end end # 参照 aggregator = PvLogAggregator.new(Time.zone.today) data = aggregator.aggregate ActiveRecordに依存がある ActiveRecordの依存をなくしBigQueryのSDKを利⽤