Rails 4:has_many中的counter_cache:通过与dependent :: destroy关联 [英] Rails 4: counter_cache in has_many :through association with dependent: :destroy

查看:82
本文介绍了Rails 4:has_many中的counter_cache:通过与dependent :: destroy关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管已经提出了类似的问题:

Although similar questions have already been asked:

  • counter_cache with has_many :through
  • dependent => destroy on a "has_many through" association
  • has_many :through with counter_cache

他们都没有真正解决我的问题.

none of them actually addresses my issue.

我有三个模型,具有一个has_many:through关联:

I have three models, with a has_many :through association :

class User < ActiveRecord::Base
  has_many :administrations
  has_many :calendars, through: :administrations
end

class Calendar < ActiveRecord::Base
  has_many :administrations
  has_many :users, through: :administrations
end

class Administration < ActiveRecord::Base
  belongs_to :user
  belongs_to :calendar
end

联接管理模型具有以下属性:

The join Administration model has the following attributes:

id
user_id
calendar_id
role

我想计算每个user有多少calendars和每个calendar有多少users.

I would like to count how many calendars each user has and how many users each calendar has.

我打算按照以下方式使用counter_cache:

I was going to go with counter_cache as follows:

class Administration < ActiveRecord::Base
  belongs_to :user, counter_cache: :count_of_calendars
  belongs_to :calendar, counter_cache: :count_of_users
end

(当然,还有相应的迁移,将:count_of_calendars添加到users表中,并且将:count_of_users添加到calendars表中.)

(and, of course, the corresponding migrations to add :count_of_calendars to the users table and :count_of_users to the calendars table.)

但是后来,我偶然发现了《 Rails指南》中的此警告:

But then, I stumbled upon this warning in Rails Guides:

4.1.2.4:从属

4.1.2.4 :dependent

如果将:dependent选项设置为:

If you set the :dependent option to:

  • :destroy,当对象被销毁时,将对其关联的对象进行销毁.
  • :delete,当对象被销毁时,所有与其关联的对象将直接从数据库中删除,而无需调用它们 销毁方法.
  • :destroy, when the object is destroyed, destroy will be called on its associated objects.
  • :delete, when the object is destroyed, all its associated objects will be deleted directly from the database without calling their destroy method.

您不应在一个名为 与另一个类上的has_many关联连接.这样做可以 导致数据库中的孤立记录.

You should not specify this option on a belongs_to association that is connected with a has_many association on the other class. Doing so can lead to orphaned records in your database.

因此,一种好方法是计算每个user有多少calendars和每个calendar有多少users?

Therefore, what would be a good practice to count how many calendars each user has and how many users each calendar has?

推荐答案

好吧,dependent: :destroy将销毁关联的记录,但不会更新counter_cache,因此您在counter_cache中的计数可能有误.相反,您可以实现一个回调,该回调将销毁关联的记录并更新您的counter_cache.

Well, dependent: :destroy will destroy the associated records, but it won't update the counter_cache, so you may have wrong count in counter_cache. Instead you can implement a callback that will destroy the associated records, and update your counter_cache.

class Calendar < ActiveRecord::Base

  has_many :administrations
  has_many :users, through: :administrations


  before_destroy :delete_dependents

  private
  def delete_dependents
    user_ids = self.user_ids
    User.delete_all(:calendar_id => self.id)
    user_ids.each do |u_id|
      Calendar.reset_counters u_id, :users
    end
  end
end

同样,也可以为User模型实现此功能

And similarly, implement this for User model too

这篇关于Rails 4:has_many中的counter_cache:通过与dependent :: destroy关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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