试图找到一种动态链接作用域的好方法

Trying to find a good way to chain scopes dynamically

我发现这篇关于清理链式作用域的文章对我正在从事的项目非常有用。但是,我的一些示波器有参数。我想不出一种方法来使它与参数一起工作。我尝试了字符串插值,我怀疑它行不通,当然也行不通。

https://www.sitepoint.com/dynamically-chain-scopes-to-clean-up-large-sql-queries/

def self.send_chain(methods)
  methods.inject(self, :send)
end

$ methods = ["with_author", "pending_review", "draft", "flagged", "published", "with_website", "with_meta_title", "with_meta_description"]
$ Article.send_chain(methods)
def send_chain(methods)
  methods.inject(self) { |result, method| result.send(*method) }
end

方法应该是一个数组,如果一个方法需要一个参数或多个参数,那应该是一个子数组

[:foo, [:bar, "argument"], :baz]