Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个Video模型和VideoComment一个与Video.
Video
VideoComment
我正在尝试查找评论最多的视频。
我怎样才能做到这一点?
你应该实现一个 counter_cache :
class Video < ActiveRecord::Base has_many :video_comments, :counter_cache => true end
您必须创建一个video_comments_count在videos表上调用的属性才能完成这项工作。然后,您将能够按此属性进行排序。
video_comments_count
videos
Rails 会在创建视频评论时自动为您增加计数器缓存,并在删除时自动减少