Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails 4作用域:在相反的作用域之间获取所有记录_Ruby On Rails_Ruby_Activerecord - Fatal编程技术网

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])}