开发者

Is there any way to order the results of a Cancan accessible_by call

开发者 https://www.devze.com 2023-02-19 02:31 出处:网络
I am using the Cancan accessible_by to retrieve a ActiveRecord::Relation result (example code below). Is there any way to order the results during the accessible_by call?

I am using the Cancan accessible_by to retrieve a ActiveRecord::Relation result (example code below). Is there any way to order the results during the accessible_by call?

UPDATE: Srdjan's was correct. @attributes was already being set using accessible_by. I have updated the example to show the sor开发者_StackOverflow中文版t by User's login. Attribute has a belongs_to relationship with 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


From the horse's mouth, as it were.

@articles = Article.accessible_by(current_ability, :update)
This is an Active Record scope so other scopes and pagination can be chained onto it.

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

Also, on the top of that page, you'll note that as of CanCan 1.4, this is done automatically when you call load_resource. Are you on that version?

0

精彩评论

暂无评论...
验证码 换一张
取 消