问题
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