0

比方說,我有一些模型Rails的型號範圍鏈接基於動態範圍名單上

class MyModel < ApplicationRecord 
    scope :opened, -> { where(status: 'open') } 
    scope :closed, -> { where(status: 'closed') } 
    scope :colored, -> { where.not(color: nil) } 
    # etc 
end 

我可以打電話範圍連鎖店如

MyModel.opened.colored 
MyModel.send('opened').send('colored') 

但我怎麼可以讓範圍鏈接基於動態範圍令牌列表?我的意思是

scopes = ['opened', 'colored', ...] 

列表可能會很長,我需要一些通用的解決方案做盡可能的簡單,就像MyModel.send_list(scopes)

回答

1

更多作爲的範圍因此,您可以添加喜歡,

scope :send_list, -> (*scopes) { scopes.inject(self) { |out, scope| out.send(scope) } } 

發送此類似YourModel.send_list(*scopes)