Skip to content

Instantly share code, notes, and snippets.

@albb0920
Last active April 3, 2019 16:50
Show Gist options
  • Save albb0920/09ff9e790d8d3d2b29dcfe98ca7f5c9e to your computer and use it in GitHub Desktop.
Save albb0920/09ff9e790d8d3d2b29dcfe98ca7f5c9e to your computer and use it in GitHub Desktop.
Unnecessary & slow query executed when using scope as condition
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
gem 'rails', '5.2.0' # use correct rails version
gem 'cancancan' # use correct cancancan version
gem 'sqlite3' # use another DB if necessary
end
require 'active_record'
require 'cancancan'
require 'cancan/model_adapters/active_record_adapter'
require 'cancan/model_adapters/active_record_4_adapter'
require 'cancan/model_adapters/active_record_5_adapter'
require 'minitest/autorun'
require 'logger'
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
# create your tables here
ActiveRecord::Schema.define do
create_table :books, force: true do |t|
t.integer :user_id
end
create_table :users, force: true do |t|
t.string :name
end
end
class Book < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :books
end
user = User.create!
5.times { Book.create(user: user) }
class Ability
include CanCan::Ability
def initialize(user)
can :read, Book, user.books do |book|
book.user_id == user.id
end
end
end
class BooksController
include CanCan::ControllerAdditions
def params
{controller: "books", action: "index"}
end
def current_user
User.first
end
end
class ActiveRecord::Associations::CollectionProxy
def load_target
raise "No need to load association"
end
end
class BugTest < Minitest::Test
def test_bug
controller = BooksController.new
resource = CanCan::ControllerResource.new(controller)
resource.load_resource
controller.instance_variable_get(:@books)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment