Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.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 针对错误属性的ActiveRecord查询_Ruby On Rails_Activerecord_Boolean - Fatal编程技术网

Ruby on rails 针对错误属性的ActiveRecord查询

Ruby on rails 针对错误属性的ActiveRecord查询,ruby-on-rails,activerecord,boolean,Ruby On Rails,Activerecord,Boolean,我不确定正确的术语是什么,所以我的谷歌搜索没有多大帮助 但无论如何,ActiveRecord允许您搜索布尔属性为TRUE的所有记录,如下所示: @current_events = Event.current 因此,返回属性“current”等于TRUE的所有事件 是否有一个漂亮的ActiveRecord方法可以返回所有为FALSE的内容?可能是这样的: @outdated_events = Event.not_current ? 如果没有,那么我可以使用“where”方法。但我只是好奇 谢谢

我不确定正确的术语是什么,所以我的谷歌搜索没有多大帮助

但无论如何,ActiveRecord允许您搜索布尔属性为TRUE的所有记录,如下所示:

@current_events = Event.current
因此,返回属性“current”等于TRUE的所有事件

是否有一个漂亮的ActiveRecord方法可以返回所有为FALSE的内容?可能是这样的:

@outdated_events = Event.not_current
?

如果没有,那么我可以使用“where”方法。但我只是好奇


谢谢

您可能将ActiveRecord“命名范围”类方法误认为是Rails功能。比如说,

class Foo < ActiveRecord::Base
  def self.current
    where current: true
  end
end

是的,你是对的。我忘了我刚才创建了一个命名范围。谢谢你纠正我!
def self.not_current
  where current: false
end