diff --git a/app/models/forum_topic.rb b/app/models/forum_topic.rb index 45cfcd5c8..b1ac36729 100644 --- a/app/models/forum_topic.rb +++ b/app/models/forum_topic.rb @@ -96,7 +96,14 @@ class ForumTopic < ActiveRecord::Base ForumTopicVisit.create(:user_id => user.id, :forum_topic_id => id, :last_read_at => updated_at) end - # user.update_attribute(:last';¬≥÷_forum_read_at, ForumTopicVisit.where(:user_id => user.id, :forum_topic_id => id).minimum(:last_read_at) || updated_at) + has_unread_topics = ForumTopic.where("forum_topics.updated_at >= ?", user.last_forum_read_at) + .joins("left join forum_topic_visits on (forum_topic_visits.forum_topic_id = forum_topics.id and forum_topic_visits.user_id = #{user.id})") + .where("(forum_topic_visits.id is null or forum_topic_visits.last_read_at < forum_topics.updated_at)") + .exists? + unless has_unread_topics + user.update_attribute(:last_forum_read_at, Time.now) + ForumTopicVisit.prune!(user) + end end end diff --git a/db/migrate/20150119191042_add_updated_at_index_to_forum_topics.rb b/db/migrate/20150119191042_add_updated_at_index_to_forum_topics.rb new file mode 100644 index 000000000..1cd7a6116 --- /dev/null +++ b/db/migrate/20150119191042_add_updated_at_index_to_forum_topics.rb @@ -0,0 +1,11 @@ +class AddUpdatedAtIndexToForumTopics < ActiveRecord::Migration + def up + execute "set statement_timeout = 0" + add_index :forum_topics, :updated_at + end + + def down + execute "set statement_timeout = 0" + remove_index :forum_topics, :updated_at + end +end