Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debug exists subquery #757

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/cancan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@
require 'cancan/model_adapters/strategies/base'
require 'cancan/model_adapters/strategies/left_join'
require 'cancan/model_adapters/strategies/subquery'
require 'cancan/model_adapters/strategies/exists_subquery'
end
22 changes: 22 additions & 0 deletions lib/cancan/model_adapters/strategies/exists_subquery.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module CanCan
module ModelAdapters
class Strategies
class ExistsSubquery < Base
def execute!
model_class.where(joined_alias_exists_subquery_inner_query.arel.exists)
end

def joined_alias_exists_subquery_inner_query
model_class
.select('1')
.left_joins(joins)
.where(*where_conditions)
.where(
"#{quoted_table_name}.#{quoted_primary_key} = " \
"#{quoted_aliased_table_name}.#{quoted_primary_key}"
)
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ class Editor < ActiveRecord::Base
if CanCan::ModelAdapters::ActiveRecordAdapter.version_greater_or_equal?('5.0.0')
describe 'selecting custom columns' do
it 'extracts custom columns correctly' do
posts = Post.accessible_by(ability).where(published: true).select('title as mytitle')
posts = Post.where(title: 'a').accessible_by(ability).where(published: true).select('title as mytitle')
puts posts.to_sql
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check output to see the issue

expect(posts[0].mytitle).to eq 'post1'
end
end
Expand Down