如何将has_scope与inherited_resources一起使用?

时间:2014-08-26 17:00:51

标签: ruby-on-rails activerecord has-scope

我关注has_scope的git wiki。

它有一个例子:

# 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)

不知道在哪里看得更远。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

你做错了。

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

这就是全部。 Inherited_resources在end_of_association_chain

中自动应用范围