评价:依赖=> :破坏 [英] Evaluating :dependent => :destroy

查看:117
本文介绍了评价:依赖=> :破坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Rails 2.2.2(红宝石1.8.7-P72),我想,以评估之前,实际上这样做破坏物体的影响。即我想是能够产生会受到所有对象的列表:依赖=>:破坏(通过一个对象的关联)。真正的问题,我试图解决的是让用户将被删除的所有内容的列表,并让他们确认操作。

In Rails 2.2.2 (ruby 1.8.7-p72), I'd like to evaluate the impact of destroying an object before actually doing it. I.e. I would like to be able to generate a list of all objects that will be affected by :dependent => :destroy (via an object's associations). The real problem I'm trying to solve is to give a user a list of everything that will be deleted and having them confirm the action.

谁能推荐一个很好的方法来进行此事?我刚刚开始寻找到的ActiveRecord ::协会,但我还没有取得很大进展。

Can anyone recommend a good way to go about this? I've just started looking into ActiveRecord::Associations, but I haven't made much headway.

更新:在我的具体情况,我已经得到了不同程度的对象(A - > B - > C)

Update: In my particular case, I've got various levels of objects (A --> B --> C).

推荐答案

这应该可以帮助您开始......很明显,我们需要自定义,但这种列出了所有协会名称是依赖销毁类BlogEntry:

This should help get you started... Obviously you'll have to customize it but this lists all association names that are dependent destroy on the class BlogEntry:

BlogEntry.reflect_on_all_associations.map do |association|
  if association.options[:dependent] == :destroy
    # do something here...
    association.name
  end
end.compact
=> [:taggings, :comments]

这篇关于评价:依赖=> :破坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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