【问题标题】:Write fixtures with counter_cache columns使用 counter_cache 列编写固定装置
【发布时间】:2015-02-19 11:07:50
【问题描述】:

编写具有counter_cache 列的fixture 既繁琐又复杂,我尝试在test_helper.rb 中使用reset_counters 方法,但没有成功。同样的方式让它工作?

【问题讨论】:

  • 请澄清您的问题。预期的输出是多少?你试过什么?
  • 如果您需要使用 counter_cache 列写入固定装置,您需要手动写入...我试过 Post.find_each { |post| Post.reset_counters(post.id, :cmets, :shares, :likes, :quotes) } 重置计数器,但它没有工作。如果我把它放在一个设置方法中..它可以工作..但它在每次测试之前执行

标签: ruby-on-rails testing fixtures counter-cache


【解决方案1】:

我使用猴子路径在测试环境中禁用 counter_cache 解决了这个问题。

config/environments/test.rb

module ActiveRecord::Associations
    class HasManyAssociation
        def has_cached_counter?(reflection = reflection())
            false
        end
    end
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    • 2015-07-28
    • 1970-01-01
    • 2018-02-10
    • 2016-12-15
    相关资源
    最近更新 更多