Rails 如何一个 scope 中 使用另一个 scope?

u1450154824 · May 11, 2016 · Last by u1453357893 replied at May 11, 2016 · 2308 hits

比如我有

scope :type_one, -> { where(type:1) }
scope :type_two, -> { where(type:2) }
scope :type_all, -> { where(type:[1,2]) }

可以有这样的类似写法吗?

scope :type_all, :type_one,:type_two

Model.type_one.type_two

#1 楼 @linyunjiang 谢谢,但是我想知道有没有我说的那一种写法。

3 Floor has deleted
4 Floor has deleted

自己写个方法就是了。

where(type:[1,2]) 是获取 type 为 1 或者 2 的数据,type_one.type_two 是获取 type 同时为 1 和 2 的数据,两者不一样。还没见过 scope :type_all, :type_one,:type_two 类似的写法。

You need to Sign in before reply, if you don't have an account, please Sign up first.