forked from rails-sqlserver/activerecord-sqlserver-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request rails-sqlserver#735 from aidanharan/fix-finder-dis…
…tinct-select-exists Order by selected items when using distinct exists
- Loading branch information
Showing
2 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
lib/active_record/connection_adapters/sqlserver/core_ext/finder_methods.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require 'active_record/relation' | ||
require 'active_record/version' | ||
|
||
module ActiveRecord | ||
module ConnectionAdapters | ||
module SQLServer | ||
module CoreExt | ||
module FinderMethods | ||
|
||
private | ||
|
||
# Same as original except we order by values in distinct select if present. | ||
def construct_relation_for_exists(conditions) | ||
if distinct_value && offset_value | ||
relation = limit!(1) | ||
|
||
if select_values.present? | ||
relation = relation.order(*select_values) | ||
else | ||
relation = relation.except(:order) | ||
end | ||
else | ||
relation = except(:select, :distinct, :order)._select!(::ActiveRecord::FinderMethods::ONE_AS_ONE).limit!(1) | ||
end | ||
|
||
case conditions | ||
when Array, Hash | ||
relation.where!(conditions) unless conditions.empty? | ||
else | ||
relation.where!(primary_key => conditions) unless conditions == :none | ||
end | ||
|
||
relation | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
ActiveSupport.on_load(:active_record) do | ||
ActiveRecord::Relation.include(ActiveRecord::ConnectionAdapters::SQLServer::CoreExt::FinderMethods) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters