1

我正在使用 Cancanaccessible_by来检索 ActiveRecord::Relation 结果(下面的示例代码)。有没有办法在accessible_by通话期间订购结果?

更新:Srdjan 是正确的。@attributes已经使用accessible_by. 我更新了示例以显示按用户登录的排序。Attribute与 有belongs_to关系User

class AttributesController < ApplicationController
  load_and_authorize_resource

  def index
    @attributes = @attributes.includes(:user).order("#{User.table_name}.login")
  end

  # GET /attribute/1
  # GET /attribute/1.xml
  def show
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @attribute }
    end
  end
end
4

1 回答 1

3

从马的嘴里说出来。

@articles = Article.accessible_by(current_ability, :update)
这是一个 Active Record 范围,因此可以将其他范围和分页链接到它上面。

来源:https ://github.com/ryanb/cancan/wiki/Fetching-Records

此外,在该页面的顶部,您会注意到从 CanCan 1.4 开始,当您调用load_resource. 你是那个版本的吗?

于 2011-03-23T20:19:04.440 回答