how to call a active record named scope with a string

后端 未结 4 1377
梦谈多话
梦谈多话 2021-02-20 14:19

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\")
         


        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-20 15:05

    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.

提交回复
热议问题