how to call a active record named scope with a string
Although @kolrie has the correct answer, it is not safe at all.
It should be whitelisted as follows:
scope = ["first_scope", "second_scope", "default_scope"].include? params[:action] ? params[:scope] : "default_scope"
@case_studies = CaseStudy.send(scope)
If some_named_scope
is a named_scope of the CaseStudy
model, you can use send
to call the method corresponding to params[:action]
value. But this is obviously heavily exploitable.
So, security aside, you could get going with:
@case_studies = CaseStudy.send(params[:action])
Hope it works.