method

compute_cache_version

Importance_0
v7.0.0 - Show latest stable - 0 notes - Class: ActiveRecord::Relation
compute_cache_version(timestamp_column) private

No documentation

This method has no description. You can help the Ruby on Rails community by adding new notes.

Hide source
# File activerecord/lib/active_record/relation.rb, line 354
    def compute_cache_version(timestamp_column) # :nodoc:
      timestamp_column = timestamp_column.to_s

      if loaded?
        size = records.size
        if size > 0
          timestamp = records.map { |record| record.read_attribute(timestamp_column) }.max
        end
      else
        collection = eager_loading? ? apply_join_dependency : self

        column = connection.visitor.compile(table[timestamp_column])
        select_values = "COUNT(*) AS #{connection.quote_column_name("size")}, MAX(%s) AS timestamp"

        if collection.has_limit_or_offset?
          query = collection.select("#{column} AS collection_cache_key_timestamp")
          query._select!(table[Arel.star]) if distinct_value && collection.select_values.empty?
          subquery_alias = "subquery_for_cache_key"
          subquery_column = "#{subquery_alias}.collection_cache_key_timestamp"
          arel = query.build_subquery(subquery_alias, select_values % subquery_column)
        else
          query = collection.unscope(:order)
          query.select_values = [select_values % column]
          arel = query.arel
        end

        size, timestamp = connection.select_rows(arel, nil).first

        if size
          column_type = klass.type_for_attribute(timestamp_column)
          timestamp = column_type.deserialize(timestamp)
        else
          size = 0
        end
      end

      if timestamp
        "#{size}-#{timestamp.utc.to_formatted_s(cache_timestamp_format)}"
      else
        "#{size}"
      end
    end
Register or log in to add new notes.