2017-02-16 30 views
0

我想根據條件鏈接我的作用域。但由於對ActiveRecord中的範圍進行了惰性評估,因此只有最後一個分配才考慮在內。相當於+ =(加上等於)以在rails中添加ActiveRecord的作用域

@list = Product.all 
if condition 1 
    @list = Product.scope1 
if condition 2 
@list = Product.scope2 
if condition 3 
    @list = Product.scope3 
... 
@list 

我希望能夠一個接一個地累積範圍。

回答

2

鏈他們這樣

@list = Product.all 
@list = @list.scope1 if condition1 
@list = @list.scope2 if condition2 
@list 

那麼到底@list擁有你想要的元素。

+1

謝謝!現在看來如此明顯,我已經得到了答案。 – Morgan