how to call a active record named scope with a string

后端 未结 4 1365
梦谈多话
梦谈多话 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条回答
  •  旧时难觅i
    2021-02-20 14:53

    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)
    

提交回复
热议问题