Ruby on rails Rails 4作用域:在相反的作用域之间获取所有记录
我有以下型号:Ruby on rails Rails 4作用域:在相反的作用域之间获取所有记录,ruby-on-rails,ruby,activerecord,Ruby On Rails,Ruby,Activerecord,我有以下型号: #models/foobar.rb class Foobar < ActiveRecord::base scope :active, ->{where(active: true)} scope :inactive, ->{where(active: false)} end 这也不起作用,因为它返回的是数组,而不是activerecord关系对象: # returns an array not an activerecord relation objec
#models/foobar.rb
class Foobar < ActiveRecord::base
scope :active, ->{where(active: true)}
scope :inactive, ->{where(active: false)}
end
这也不起作用,因为它返回的是数组,而不是activerecord关系对象:
# returns an array not an activerecord relation object
Foobar.active + Foobar.inactive
如何使activerecord关系对象同时包含活动和非活动foobar记录?更好的方法是:有没有一种方法可以为我创建一个作用域?您可以使用以下代码:
scope :active_and_inactive, ->{where(active: [true,false])}
通过使用数组,您可以为一个属性传递多个值。我使用status而不是boolean表示active,作用域如下:
scope:active,->{where(status:“active”)}
scope :active_and_inactive, ->{where(active: [true,false])}