开发者

display all available named scopes of a model in Ruby on Rails

开发者 https://www.devze.com 2022-12-29 23:49 出处:网络
Especially when you are using Searchlogic. It is kind开发者_如何学编程a hard for me to guess what named scope to use to achieve what I need.You can get all defined named scopes for a model, using

Especially when you are using Searchlogic. It is kind开发者_如何学编程a hard for me to guess what named scope to use to achieve what I need.


You can get all defined named scopes for a model, using

Model.scopes
=> {:one_scope_name=>#<Proc:0x0000...>, :other_scope_name=>#<Proc:0x0000...>, ... }

It gives you a hash, so if you want only names, use Model.scopes.keys

But this will give you only scopes, that are already defined. It won't help you will scopes, generated by searchlogic, because, as you can read in documentation (Under the hood section), scopes like field_eq etc are created only when they are first used:

"Searchlogic utilizes method_missing to create all of these named scopes. When it hits method_missing it creates a named scope to ensure it will never hit method missing for that named scope again. Sort of a caching mechanism. It works in the same fashion as ActiveRecord’s “find_by_*” methods. This way only the named scopes you need are created and nothing more."

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号