【发布时间】:2020-03-07 02:50:01
【问题描述】:
我有一个 has_many :images, through: :gallery 的零件模型,我想实现 counter_cache 来存储零件的图像计数。这是我的设置。
部分
has_one :gallery, dependent: :destroy
has_many :images, through: :gallery
图库
belongs_to :part
has_many :images
图片
belongs_to :gallery
我可以使用@part.images 获取零件的图像,现在我想缓存图像计数,以便我可以使用@part.images.size 甚至通过images_count 订购零件。我通常会在belongs_to 一侧使用counter_cache: true 来执行此操作,但在这种情况下我该怎么做呢?有可能吗?
【问题讨论】:
-
那些答案怎么样 - stackoverflow.com/questions/8991118/… ?问题和你的差不多
-
谢谢,该线程中的第二个答案帮助我解决了这个问题。
标签: ruby-on-rails counter-cache