0

는 이제 내가레일 모델 범위는

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

같은 범위 체인을 호출 할 수 있습니다

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

몇 가지 모델을 가지고 있다고 가정 해 봅시다 동적 범위 이름 목록을 기반으로 체인하지만 어떻게 내가 동적 범위에 따라 체인 범위를 만들 수 있습니다 토큰리스트? 나는 매우 긴 될 수있다

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

목록을 의미 내가 MyModel.send_list(scopes) 같은, 가능한 한 간단하게 할 몇 가지 일반적인 솔루션이 필요합니다.

답변

1

더 많은 범위의 결과로, 당신이 좋아하는 추가 할 수

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

전송이 같은 YourModel.send_list(*scopes)