0

我有一个counter_culture计数器,它取决于是否定义了回形针附件:

class Post < ::ActiveRecord::Base
  belongs_to :user

  counter_culture :user, column_name: Proc.new { |p| p.media? ? 'posts_with_photo_count' : nil }
end

问题是当帖子更新销毁时计数器不会更新。

我想这应该与Paperclip自己的回调系统有关。

4

1 回答 1

0

我猜到发生了什么。这实际上是由于counter_culturePaperclip的回调的流不匹配造成的。

由于 on after_update,当计数器条件执行时,附件尚未处理,p.media?返回 false 并且计数器不递增。

类似的事情发生在 上after_destroy,因为附件在 上被破坏before_destroy

针对这个问题,我提出了以下解决方案:

counter_culture :user, column_name: Proc.new { |p| (p.photo_file_name? || !p.photo_file_name_was.nil?)  ? 'posts_with_photo_count' : nil }

它基本上考虑_file_name创建和更新,并验证附件是否在销毁时被清除。

于 2015-10-23T12:53:26.393 回答