创建一个Post模型,其中包含lock_Version和Comments_Count列,以及带有:Comments的Has_Many关系. 使用COUNTER_CACHE=TRUE,创建一个带有lock_version列的注释模型,并与:POST建立一个beles_to关系.
class Post < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post, counter_cache: true
end
post = Post.create!
comment = Comment.create(post)
puts post.reload.comments_count # 1 (correct)
Comment.last.destroy
puts post.reload.comments_count # 1 (should be 0 - incorrect)
我试图更改Rails和Ruby版本,但都失败了.