如何在ActiveRecord 3.x中获得所有范围列表 [英] How can I get all list of scopes in ActiveRecord 3.x
本文介绍了如何在ActiveRecord 3.x中获得所有范围列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将ActiveRecord与Rails 3一起使用。
I am using ActiveRecord with Rails 3.
我在模型中定义了范围。如何获得该模型所有作用域的列表?
I defined scopes in my model. How can I get the list of all scopes of that model?
以前我可以使用 Model.scopes
OR
是否可以检查范围是否已定义?像 Model.scope_defined?( scope_name)
类似。
推荐答案
您可以查看是否以这种方式定义了范围
You can see if a scope is defined or not this way
Model.send(:valid_scope_name?, :scope_name)
它将返回 true
(如果确实存在), nil
(如果不存在)。
it will return true
if it does exist and nil
if it does not.
如果您检查 valid_scope_name?
,您看到可以使用 respond_to测试它了?
然后避免使用日志记录部分。
If you check the source code of valid_scope_name?
, you see that you can just test it using respond_to?
and then avoid the logging part.
Model.respond_to?(scope_name, true)
这篇关于如何在ActiveRecord 3.x中获得所有范围列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文