Nested resources with CanCan 2.0 and Mongoid #629

Closed
pbougie opened this Issue May 26, 2012 · 4 comments

Comments

Projects
None yet
4 participants

pbougie commented May 26, 2012

I'm currently having problems with nested resources using CanCan 2.0 and Mongoid.

I have the following nested routes:

resources :employees do
  resources :timeoffs, :controller => "employees/timeoffs"
end

The models are as follows:

class Employee
  include Mongoid::Document
  has_many :timeoffs

  ...
end

class Timeoff
  include Mongoid::Document
  belongs_to :employee

  ...
end

And the Employees::Timeoffs controller is:

class Employees::TimeoffsController < ApplicationController
  load_and_authorize_resource :employee
  load_and_authorize_resource :through => :employee

  def index
  end

  ...
end

And the Ability model has these rules (simplified for now):

can :read, :employees
can :access, :'employees/timeoffs'

With this set up, it displays the index action but the problem is that it returns no results for the Timeoff model. It keeps injecting where(:_id => {'$exists' => false, '$type' => 7}) into the MongoDB queries. See this line of code for more information. Any ideas what I'm missing?

Thanks.

Collaborator

andhapp commented May 27, 2012

@pbougie Is :access a custom action that you've defined? The default actions provided by CanCan are are the Restful ones. Also, the line that mention will only execute if there aren't any rules. Are the other rules working okay?

Update: My bad, :access is part of CanCan 2.0 which I didn't realise earlier.

ollym commented Jun 5, 2012

See #635. Nested resources were broken in 2.0

exocode commented Jul 4, 2012

I have some troubles with cancan and mongoid
(sorry, I am a Newbie)

undefined method `reflect_on_association' for "4ff484be34257be50d00000a":String

My Model (self referencing):


class Musicstyle
  include Mongoid::Document

  field :title, type: String
  field :description, type: String
  recursively_embeds_many
  validates_presence_of :title
  validates_uniqueness_of :title
end

Controller:

load_and_authorize_resource
def create
    if params[:musicstyle][:parent_musicstyle] = nil
      parent = Musicstyle.find(params[:musicstyle].delete(:parent_musicstyle))
      parent.child_musicstyles.create(params[:musicstyle])
    end

    @musicstyle = Musicstyle.new(params[:musicstyle])
end
end

I found out, that when I remove the load_and_authorize_resource function from the controller -> all worked perfectly

I am not sure I am done something wrong or cancan or mongoid raises this issue....

Any help will be appriciate

I am using mongoid (2.4.10) and cancan (1.6.7)

Collaborator

andhapp commented Jul 5, 2012

@Klangfeld: I've created a fresh issue (#682) since this one was for 2.0 and you are having the same issue in 1.6.7. I guess we need to backport the fix.

Will close this one as this was fixed in 2.0 by @ollym's pull request.

andhapp closed this Jul 5, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment