如何列出所有模型,包括关注点 [英] How to list all models including a concern

查看:28
本文介绍了如何列出所有模型,包括关注点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法可以列出包含特定关注点的所有模型的类名?

Is there a simple way to list the class names of all models that include a particular Concern?

类似于:

ActiveRecord.models.select{ |m| m.included_modules.include? MyConcernModule }

推荐答案

我有一个名为Concerns::CoursePhotoable"的问题.这是包含它的两个模型:

I have a concern named "Concerns::CoursePhotoable". Here's the two models that include it:

> ActiveRecord::Base.descendants.select{|c| \
     c.included_modules.include?(Concerns::CoursePhotoable)}.map(&:name)
=> ["Course", "ProviderCourse"]

澄清一下,我的关注点实际上被命名为关注点::CoursePhotoable".如果您的名为Fooable",您只需将Fooable"放在我有Concerns::CoursePhotoable"的地方.我命名了我的顾虑,以避免与可寻址"发生冲突.

To clarify, my concern is really named "Concerns::CoursePhotoable". If yours was named "Fooable" you'd simply put "Fooable" where I have "Concerns::CoursePhotoable". I namespace my concerns to avoid conflicts with say "Addressable".

当前版本的 Rails 使用 include?.较早使用的include.

Current versions of Rails use include?. Older used include.

这篇关于如何列出所有模型,包括关注点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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