问题的has_many:通过,缓存,触摸和counter_cache [英] Issues with has_many :through, cache, touch and counter_cache

查看:118
本文介绍了问题的has_many:通过,缓存,触摸和counter_cache的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多的has_many的:通过我的应用程序的关系。我extensivley示出与此相关的信息,如连接的对象的数目。每当用户更新的关系,连接表被修改了,我能赶上这个我扫地机。

I have a lot of has_many :through relations in my app. I am extensivley showing informations related to this, such as number of connected objects. Whenever user updates the relation, join table is modified, and I can catch this my Sweepers.

现在的问题是,该连接表项的删除,不是破坏。如果关系走了,我也没有合理的办法来检测这一点,我是从缓存中显示误导性的信息。喜欢一切:触摸=> true,或者:counter_cache =>杰作partialy。这是得到增加,如果关系被更新或创建的。但是,如果用户删除关系没有任何反应。 :counter_cache是​​越来越坏了,:触摸不会触发。

The problem is, that join table entries are deleted, not destroyed. If relation is gone, I have no resonable way to detect this, and I am displaying misleading informations from the cache. Everything like :touch => true, or :counter_cache => true works partialy. It's get incremented if relations are updated or created. But if user removes relation nothing happens. :counter_cache is getting broken, :touch doesn't trigger.

垃圾溶液是调用.touch在控制器中,当主模型被保存。这类作品,但它似乎真的不专业。这应该是在模型逻辑,而不是在控制器

The garbage solution is to call .touch in the controller, when the main model is saved. This kind of works, but it seems really non-professional. This should be in the model logic, not in the controllers.

我觉得我失去了一些东西很大,但不能让我的头了这一点。任何人都可以把一些有识之士对这个问题?

I feel like I am missing something big, but cant get my head over this. Anyone could put some insight on this problem?

推荐答案

猴子打补丁活动记录是没有必要的。当定义你的公会,将:依赖选项:破坏

Monkey patching Active Record isn't necessary. When defining your association, set the :dependent option to :destroy.

class Book < ActiveRecord::Base
  has_many :authorships, :dependent => :destroy
  has_many :authors, :through => :authorships, :dependent => :destroy
end

这篇关于问题的has_many:通过,缓存,触摸和counter_cache的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆