How to return an empty ActiveRecord relation?

前端 未结 9 1969
有刺的猬
有刺的猬 2020-12-07 07:38

If I have a scope with a lambda and it takes an argument, depending on the value of the argument, I might know that there will not be any matches, but I still want to return

9条回答
  •  再見小時候
    2020-12-07 08:24

    scope :none, limit(0)
    

    Is a dangerous solution because your scope might be chained upon.

    User.none.first

    will return the first user. It's safer to use

    scope :none, where('1 = 0')
    

提交回复
热议问题