Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Cannot define abilities with multiple association hash conditions #965

Open
knoopx opened this Issue · 0 comments

1 participant

@knoopx

Example

can :read, Comment, post: {private: false, author_id: author.id}

Raises NoMethodError: undefined method table_name for nil:NilClass

Spec

gem 'activerecord', '~> 3.0'
gem 'cancan', '~> 1.6'

require 'active_record'
require 'cancan'

ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"

ActiveRecord::Schema.define do
  create_table :authors do |t|
    t.string :name, null: false
  end

  create_table :posts do |t|
    t.integer :author_id, null: false
    t.string :title
    t.boolean :private, default: false
  end

  create_table :comments do |t|
    t.integer :post_id, null: false
    t.text :body
    t.boolean :private, default: false
  end
end

class Author < ActiveRecord::Base
  has_many :posts
end

class Post < ActiveRecord::Base
  belongs_to :author
  has_many :comments
end

class Comment < ActiveRecord::Base
  belongs_to :post
end

class Ability
  include CanCan::Ability

  def initialize(author)
    author ||= Author.new
    can :read, Comment, post: {private: false, author_id: author.id}
  end
end


describe "Author Permissions" do
  let(:author) { Author.create!(name: "Author") }
  let(:post) { Post.create!(title: "Post", author: author) }
  let(:comment) { Comment.create!(body: "Comment", post: post) }

  subject { Ability.new(author) }

  example do
    Comment.accessible_by(subject).should include(comment)
  end
end

Output

-- create_table(:authors)
   -> 0.0116s
-- create_table(:posts)
   -> 0.0027s
-- create_table(:comments)
   -> 0.0011s
  1) Author Permissions multiple hash conditions
     Failure/Error: Comment.accessible_by(subject).should include(comment)
     NoMethodError:
       undefined method `table_name' for nil:NilClass
     # ./cancan_spec.rb:60:in `block (2 levels) in <top (required)>'

 1/1 |====================================================================================================================================================================== 100 =======================================================================================================================================================================>| Time: 00:00:00 

Top 1 slowest examples (0.01371 seconds, 100.0% of total time):
  Author Permissions multiple hash conditions
    0.01371 seconds ./cancan_spec.rb:59

Finished in 0.01523 seconds
1 example, 1 failure

Failed examples:

rspec ./cancan_spec.rb:59 # Author Permissions multiple hash conditions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.