mirror of
https://github.com/discourse/discourse.git
synced 2025-02-06 19:38:24 +00:00
The `posts` relation on `Topic` is not ordered. Using `Topic.posts.first` is basically the same as asking for a random post, it will depend on DB order. This breaks on Topic merge and split for example. Additionally, a huge problem with that is that it forces active record down a slow path. `Topic.posts.first` is extremely slow on giant topics, since it has no default ordering it appears AR materializes the entire set prior to doing `first`. This commit also illustrates the importance of testing, initially I only fixed the second instance of the problem in `post_validator.rb` but testing revealed that the problem was repeated at the top of the file. Longer term we should consider a larger change of default ordering the posts relations so people do not fall down this trap anymore.