Skip to content

Commit

Permalink
Merge pull request #512 from CanCanCommunity/kaspernj-optimised-queri…
Browse files Browse the repository at this point in the history
…es-with-left-joins

Kaspernj optimised queries with left joins
  • Loading branch information
coorasse authored Jun 22, 2018
2 parents e271862 + e925e41 commit 271b3fb
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Unreleased

* Drop support for actions without a subject (andrew-aladev)
* Allow to add attribute-level rules (phaedryx)
* Removed eager loading of associations for ActiveRecord 5 (kaspernj)

2.2.0 (Apr 13th, 2018)

Expand Down
6 changes: 1 addition & 5 deletions lib/cancan/model_adapters/active_record_5_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,9 @@ def self.matches_condition?(subject, name, value)

private

# As of rails 4, `includes()` no longer causes active record to
# look inside the where clause to decide to outer join tables
# you're using in the where. Instead, `references()` is required
# in addition to `includes()` to force the outer join.
def build_relation(*where_conditions)
relation = @model_class.where(*where_conditions)
relation = relation.includes(joins).references(joins) if joins.present?
relation = relation.left_joins(joins).distinct if joins.present?
relation
end

Expand Down
1 change: 0 additions & 1 deletion spec/cancan/model_adapters/active_record_4_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ class Child < ActiveRecord::Base
parent = Parent.create!
Child.create!(parent: parent, created_at: 1.hours.ago)
Child.create!(parent: parent, created_at: 2.hours.ago)

expect(Parent.accessible_by(@ability)).to eq([parent])
end
end
Expand Down

0 comments on commit 271b3fb

Please sign in to comment.