Walter Lee Davis
unread,Mar 17, 2019, 3:56:05 PM3/17/19Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to rubyonra...@googlegroups.com
I want to (in a metaprogramming context) invoke a scope on an ActiveRecord based model. I know the name of the scope, but I don't want to use `send` to do this, because send can be evil. I know that ActiveRecord defines a class method named scope that gathers up these scopes somewhere internally, but I can't figure out where that is, or how you can pick one out of the stack to execute.
What is the best equivalent to this (completely made-up example):
def call_scope(model, scope = 'all')
model.send scope.to_sym
end
The key feature is that we late-evaluate which model and which scope, so it can be used inside an enumerator, and doesn't rely on knowing the exact parties in play.
Would this be a good place for class_eval? (I just tried that, and it works)
def call_scope(model, scope = 'all')
model.class_eval scope.to_s
end
Is there something more Rails-like I could/should use?
Am I wrong about send?
Thanks in advance,
Walter