I didn't give it too much thought, and I never tried it myself, but you might do the following:
class TimestampAsCacheKey
def initialize(prefix,timestamp)
@key = prefix + timestamp.to_s
end
def cache_key # This is what Rails calls to get a key from object for caching
@key
end
end
...
# in your model:
ts = ... # somehow get the most recent timestamp of your projects, like
# 1) either @projects.to_a.map(&:updated_at).max
# or from `projects` table, like
# Project.order("updated_at DESC").first.updated_at
# (you should further optimize the query, of course, but you get the idea)
# but probably not, since the whole point is to avoid hitting db
# 2) or, better yet, from storing it in a global var, special DB table, etc
# (just update it there each time you save a project,
# e.g. through after_save / after_commit)
@projects_last_updated_time = TimestampAsCacheKey.new("projects-timestamped-list-",ts)
And then in your view:
<% cache @projects_last_updated_time do %>
<% @projects.each do |project| %>
...
<% end %>
<% end %>