Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Spec中计数器缓存的奇怪行为_Ruby On Rails_Activerecord_Ruby On Rails 4_Rspec_Counter Cache - Fatal编程技术网

Ruby on rails Spec中计数器缓存的奇怪行为

Ruby on rails Spec中计数器缓存的奇怪行为,ruby-on-rails,activerecord,ruby-on-rails-4,rspec,counter-cache,Ruby On Rails,Activerecord,Ruby On Rails 4,Rspec,Counter Cache,我在Gallery上设置了一个标准计数器缓存来跟踪它的照片。计数器缓存按照Rails默认的photos\u count命名 在我的Gallery spec中,我测试计数器缓存,如下所示: it "updates 'photos_count' counter cache" do gallery = create(:gallery_with_photos, with_photos_count: 3) gallery.reload # Fails without this

我在Gallery上设置了一个标准计数器缓存来跟踪它的照片。计数器缓存按照Rails默认的
photos\u count
命名

在我的Gallery spec中,我测试计数器缓存,如下所示:

  it "updates 'photos_count' counter cache" do

    gallery = create(:gallery_with_photos, with_photos_count: 3)
    gallery.reload # Fails without this
    expect(gallery.photos_count).to eq 3


    expect {
      gallery.photos.first.destroy
      gallery.reload # Fails without this
      }.to change{ gallery.photos_count }.by(-1)

    gallery.photos << create(:photo)

    expect {
      gallery.save!
      gallery.reload # Fails without this
      }.to change { gallery.photos_count }.by(1)

  end
它“更新”照片计数“计数器缓存”吗
gallery=创建(:gallery_with_photos,with_photos_count:3)
gallery.reload#没有此选项将失败
期望(画廊照片数量)达到等式3
期待{
画廊。照片。第一。销毁
gallery.reload#没有此选项将失败
}。更改{gallery.photos_count}.by(-1)

gallery.photos发生这种情况是因为计数器更新是在查询之后执行的,您需要从数据库中重新提取对象以查看效果

它仅在测试环境中需要

这里也有关于这一点的讨论:

发生这种情况是因为计数器更新是在查询之后执行的,您需要从数据库中重新提取对象以查看效果

它仅在测试环境中需要

这里也有关于这一点的讨论:

发生这种情况是因为计数器更新是在查询之后执行的,您需要从数据库中重新提取对象以查看效果

它仅在测试环境中需要

这里也有关于这一点的讨论:

发生这种情况是因为计数器更新是在查询之后执行的,您需要从数据库中重新提取对象以查看效果

它仅在测试环境中需要

这里也有关于这一点的讨论:

是的,我已经读过这个问题,但是在他的回答的第二部分中没有使用
重新加载
。区别在于,在另一个示例中,计数是通过
User.last.media\u count
检索的,这会强制进行另一个查询。您的示例测试了
实例中的缓存值。是的,我读过这个问题,但是在他的回答的第二部分中没有使用
重新加载
。区别在于,在另一个示例中,计数是通过
User.last.media\u count
检索的,这会强制进行另一个查询。您的示例测试了
实例中的缓存值。是的,我读过这个问题,但是在他的回答的第二部分中没有使用
重新加载
。区别在于,在另一个示例中,计数是通过
User.last.media\u count
检索的,这会强制进行另一个查询。您的示例测试了
实例中的缓存值。是的,我读过这个问题,但是在他的回答的第二部分中没有使用
重新加载
。区别在于,在另一个示例中,计数是通过
User.last.media\u count
检索的,这会强制进行另一个查询。您的示例测试
实例中的缓存值。