How can I get all list of scopes in ActiveRecord 3.x

旧城冷巷雨未停 提交于 2020-01-24 02:50:07

问题


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?

Previously I could use Model.scopes

OR Can I check a scope is defined or not? Something like Model.scope_defined?("scope_name")

Thanks in advance.


回答1:


You can see if a scope is defined or not this way

Model.send(:valid_scope_name?, :scope_name)

it will return true if it does exist and nil if it does not.

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)


来源:https://stackoverflow.com/questions/12616175/how-can-i-get-all-list-of-scopes-in-activerecord-3-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!