根据Rails的documentation for scope
,一个范围,如:为什么这个范围类查询方法使用拒绝?
class Shirt < ActiveRecord::Base
scope :red, -> { where(color: 'red') }
end
是真正:
class Shirt < ActiveRecord::Base
def self.red
where(color: 'red')
end
end
他们也说关系应作为一个Array
,所以做这样的事情
Shirt.red.each(&block)
应该工作......并且它确实如此。
使用我们上面所知的一切,为什么以下不起作用?
class Shirt < ActiveRecord::Base
def self.short_sleeved
reject{|object| object.short_sleeved == false}
end
end
Shirt.red.short_sleeved
结果undefined method 'reject' for #<Class:0xba552d4>
我不知道你正在尝试做? 'class Shirt {where(short_sleeve:true)} end'''将会做到 –
MZaragoza
根据APIDock,我的语法是假设工作...但是我发现一个线程在轨道git存储库已给出相同的错误。解决的办法是把所有的东西都放在枚举器上... all.reject {} https://github.com/rails/rails/issues/21943 –