如何调用一个活动记录命名字符串范围 [英] how to call a active record named scope with a string
问题描述
我敢肯定,我错过理解使用电话,但我想我可以做这样的事情。
I'm sure I'm miss understanding the use of call but I thought I could do something like this.
@case_studies = CaseStudy.call("some_named_scope")
在哪里some_named_scope也是在案例研究一个名为范围。我之所以需要使用电话是因为我有一个名为范围是在控制器的动作的名称相同,所以我跳槽到做这样的事情。
Where "some_named_scope" is also a named scope in CaseStudy. The reason why I need to use call is because I have named scopes that are the same names of the actions in the controller so I'm hopping to do something like this.
@case_studies = CaseStudy.call(params[:action])
:::原谅我,我才意识到我在想的发送方法,一些如何字呼叫被困在我的脑海。但@case_studies = CaseStudy.send(PARAMS [:行动])的作品,因为我认为它会。
::: forgive me, I just realized I was thinking about the send method, some how the word call got stuck in my head. but @case_studies = CaseStudy.send(params[:action]) works as I thought it would.
推荐答案
如果 some_named_scope
是 named_scope 的案例研究
模式,您可以使用发送
来调用相应的 PARAMS方法[:行动]
价值。但是,这显然是严重攻击。
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])
希望工程。
这篇关于如何调用一个活动记录命名字符串范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!