Sam Kong
Sam Kong

Reputation: 5840

How do I combine scopes in rails?

If I have 2 scopes for Product class, I can do like this.

Product.best.sale #retrieves best and sale

For some reason, I have 2 seperated scopes like the following.

a = Product.best
b = Product.sale
a.b #ERROR

I tried a.b and it gives an error.

How do I combine a and b to get the Product.best.sale?

Thanks.

Sam

Upvotes: 1

Views: 819

Answers (1)

Dylan Markow
Dylan Markow

Reputation: 124469

When assigning b, you should be chaining the scope onto a, not a new Product call:

a = Product.best
b = a.sale
a.b
# => Retrieves best and sale

Upvotes: 4

Related Questions