method

compute_cache_version

Importance_0
v6.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 345
    def compute_cache_version(timestamp_column) # :nodoc:
      if loaded? || distinct_value
        size = records.size
        if size > 0
          timestamp = max_by(&timestamp_column)._read_attribute(timestamp_column)
        end
      else
        collection = eager_loading? ? apply_join_dependency : self

        column = connection.visitor.compile(arel_attribute(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")
          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

        result = connection.select_one(arel, nil)

        if result
          column_type = klass.type_for_attribute(timestamp_column)
          timestamp = column_type.deserialize(result["timestamp"])
          size = result["size"]
        else
          timestamp = nil
          size = 0
        end
      end

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