0

我正在关注 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)

不知道在哪里进一步寻找。有任何想法吗?

4

1 回答 1

0

你做错了。

private
  def collection
    @accounts ||= end_of_association_chain.order(created_at: :desc)
  end

这就是全部。Inherited_resources 在 end_of_association_chain 内自动应用范围

于 2014-09-25T07:56:33.520 回答