Nested Resources Broken in 2.0 Master #633

ollym opened this Issue May 29, 2012 · 9 comments

2 participants


Don't ask me why or how, I've been using 2.0 for a while with heavy use of nested resources but all of a sudden it started failing. I created a new project just to test and it continues to fail.

Rails 3.2.3 + Ruby 1.9.3 + CanCan 2.0


gem 'cancan',
  git: 'git://',
  ref: '2.0'

Simple setup:

rails g scaffold user name email
rails g scaffold post user:references title body:text


resources :users do
  resources :posts


class PostsController < ApplicationController

  load_and_authorize_resource :user
  load_and_authorize_resource :post, through: :user


rails s create a couple of users, then go to one and then to localhost:3000/users/1/posts

ActiveRecord::RecordNotFound in PostsController#index

Couldn't find Post with id=1

ey? parameters in the error report give: {"user_id"=>"1"}

Why is it looking for a post with id 1? It should be looking for a user with that id!!!!!
What am I doing wrong?


@ollym Thanks for a detailed bug report. I am going to create the sample app on my local and debug this issue. Thanks


Just to confirm, the identical code works fine on the current master release. (ie. 1.x not 2.x)
I've also tried adding all the parameters manually:

load_and_authorize_resource :user, class_name: User, instance_variable: :user, parent: true
load_and_authorize_resource :post, through: :user, class_name: Post, instance_variable: :post, parent: false

I get the same error as mentioned above.


And the relavent stack trace:

activerecord (3.2.3) lib/active_record/querying.rb:5:in `find'
.../cancan-ccd24ab30fdd/lib/cancan/model_adapters/abstract_adapter.rb:20:in `find'
.../cancan-ccd24ab30fdd/lib/cancan/controller_resource.rb:117:in `find_resource'
.../cancan-ccd24ab30fdd/lib/cancan/controller_resource.rb:98:in `find_and_update_resource'
.../cancan-ccd24ab30fdd/lib/cancan/controller_resource.rb:66:in `load_resource_instance'
.../cancan-ccd24ab30fdd/lib/cancan/controller_resource.rb:24:in `process'
.../cancan-ccd24ab30fdd/lib/cancan/controller_resource.rb:10:in `block in add_before_filter'
activesupport (3.2.3) lib/active_support/callbacks.rb:418:in `_run__3637987228216583714__process_action__1929643210915813743__callbacks'

Found the problem.

Uses namespaced_name, defined here:

Which deduces the class name from the controller, which is incorrect for nested resources. It should be taken from the resource name instead.

# when nil    then namespaced_name.to_s.camelize.constantize
when nil    then name.to_s.classify.constantize

Should fix it. I haven't tested it though, do you want me to work on a patch? I'm very surprised nested resources isn't part of the unit testing!


2.0 is kind of experimental at the moment.

I'm not sure why the code is the way it is. Please create a faling test case and then make it pass. Once you've done that send a pull request. If you need help with any of that, just let us know. Thanks.


It was introduced here:

Not sure why this guy ever got the commit pulled, it's a pretty serious break. I spent hours debugging.


Well, we all make mistakes and these oversights are possible. I know sometimes it's annoying when you just want to get on with your actual work. This is the fun of working with OS code. It works 99% of the time without issues but sometimes you hit a roadblock.

Will you be able to create a pull request?

@andhapp andhapp was assigned May 30, 2012

Haha yea sure. sorry for moaning about it. Will fix it now


Closing this as pull request has been created already and will be merged in shortly.

@andhapp andhapp closed this Jun 2, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment