我正在关注 git wiki has_scope
。
它有一个例子:
# in model
scope :featured, -> whatever
# in controller
has_scope :by_degree
...
@graduations = apply_scopes(Graduation).all
这样做了:
class Account < ActiveRecord::Base
scope :inactive, -> { where(deleted_at: nil) }
end
class Admin::AccountsController < Admin::BaseController
has_scope :inactive
private
def collection
apply_scopes(Account)
end
def end_of_association_chain
@end_of_association_chain ||= super.order(created_at: :desc)
end
end
在视图中访问集合时(如collection.each
) - 出现此错误:
undefined method `each' for #<Class:0x007fbeca533eb0>
好像要上课了。但我希望它包含一组对象。
尝试添加load
方法:
collection.load.each ...
但是又出现了一个错误:
wrong number of arguments (0 for 1..2)
不知道在哪里进一步寻找。有任何想法吗?