简体   繁体   English

如何将has_scope与Inherited_resources一起使用?

[英]How to use has_scope with inherited_resources?

I'm following git wiki for has_scope . 我正在关注git wiki的has_scope

It has an example: 它有一个例子:

# in model
scope :featured, -> whatever

# in controller
has_scope :by_degree
...
@graduations = apply_scopes(Graduation).all

So did that: 也是这样:

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

When accessing collection in the view (like collection.each ) - getting this error: 在视图中访问collection时(例如collection.each )-出现此错误:

undefined method `each' for #<Class:0x007fbeca533eb0>

It seems to give a class. 好像上课了。 But I expect it to contain an array of objects. 但我希望它包含一个对象数组。

Tried to add the load method: 尝试添加load方法:

collection.load.each ...

But got another error: 但是又出现了另一个错误:

wrong number of arguments (0 for 1..2)

Have no idea where to look further. 不知道在哪里进一步看。 Any ideas? 有任何想法吗?

You did this wrong. 你做错了

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

And this is all. 这就是全部。 Inherited_resources apply scopes automatically inside of end_of_association_chain Inherited_resources在end_of_association_chain内部自动应用范围

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM