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

Adding Table Context to Order Hash #12610

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 30 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -862,4 +862,34 @@

*Slava Markevich*

* Prepend original table name to order hash
Copy link
Contributor

Choose a reason for hiding this comment

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

Please move this changelog entry to the first line


With the models

class Post
has_many :comments
scope :dewey, -> { order(subject: :desc) }
end

class Comment
belongs_to :post
end

And records

p1 = Post.create(subject: 'Awesomeness')
p2 = Post.create(subject: 'Zaniness')
c1 = Comment.create(type: 'Cool', post_id: p1.id)
c2 = Comment.create(type: 'Cool', post_id: p2.id)

The call

Comment.where(type: 'Cool').joins(:post).merge(Post.dewey)

Should yield

[c2, c1]

*Paul Pettengill*

Please check [4-0-stable](https://github.com/rails/rails/blob/4-0-stable/activerecord/CHANGELOG.md) for previous changes.
21 changes: 18 additions & 3 deletions activerecord/lib/active_record/relation/query_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,7 @@ def build_order(arel)
when Symbol
table[order].asc
when Hash
order.map { |field, dir| table[field].send(dir) }
order.values[0] == :asc ? table[order].asc : table[order].desc
else
order
end
Expand All @@ -1037,9 +1037,24 @@ def preprocess_order_args(order_args)
references.map! { |arg| arg =~ /^([a-zA-Z]\w*)\.(\w+)/ && $1 }.compact!
references!(references) if references.any?

# if a symbol is given we prepend the quoted table name
# if a symbol or hash is given we prepend the quoted table name
order_args.map! do |arg|
arg.is_a?(Symbol) ? Arel::Nodes::Ascending.new(klass.arel_table[arg]) : arg
if arg.is_a?(Symbol)
prepend_table_sort(arg, :asc)
elsif arg.is_a?(Hash)
prepend_table_sort(arg.keys[0], arg.values[0])
else
arg
end
end
end

def prepend_table_sort(field, direction)
case direction
when :asc
Arel::Nodes::Ascending.new(klass.arel_table[field])
when :desc
Arel::Nodes::Descending.new(klass.arel_table[field])
end
end

Expand Down
19 changes: 19 additions & 0 deletions activerecord/test/cases/scoping/relation_scoping_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,25 @@ def test_merge_inner_scope_has_priority
end
end

def test_merge_order_hash
merge_sql = Comment.where(type: "Comment").joins(:post).
merge(Post.ranked_by_comments_hash).to_sql
assert_match '"posts"."comments_count" DESC', merge_sql

merge_results = Comment.where(type: "Comment").joins(:post).
merge(Post.ranked_by_comments_hash).
order("comments.id")
c1 = Comment.joins(:post).find(1)
c2 = Comment.joins(:post).find(2)
c8 = Comment.joins(:post).find(8)
c9 = Comment.joins(:post).find(9)
assert_equal merge_results.size, 4
assert_equal merge_results.first, c8
assert_equal merge_results.limit(2).last, c9
assert_equal merge_results.limit(3).last, c1
assert_equal merge_results.last, c2
end

def test_replace_options
Developer.where(:name => 'David').scoping do
Developer.unscoped do
Expand Down
2 changes: 2 additions & 0 deletions activerecord/test/fixtures/posts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ authorless:
sti_comments:
id: 4
author_id: 1
comments_count: 4
title: sti comments
body: hello
type: Post

sti_post_and_comments:
id: 5
author_id: 1
comments_count: 3
title: sti me
body: hello
type: StiPost
Expand Down
3 changes: 3 additions & 0 deletions activerecord/test/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def greeting

scope :containing_the_letter_a, -> { where("body LIKE '%a%'") }
scope :ranked_by_comments, -> { order("comments_count DESC") }
scope :ranked_by_comments_hash, -> { order(comments_count: :desc) }
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we could add a new test case for hashes with multiple elements. WDYT?

Copy link
Author

Choose a reason for hiding this comment

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

Makes sense to me, and would probably break my fix, assuming you mean:

scope :ranked_by_comments_hash, -> { order(comments_count: :desc, subject: :asc) }

Copy link
Member

Choose a reason for hiding this comment

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

Yes. this is what I'm meaning.


scope :limit_by, lambda {|l| limit(l) }

Expand All @@ -40,6 +41,8 @@ def first_comment
scope :with_comments, -> { preload(:comments) }
scope :with_tags, -> { preload(:taggings) }

scope :comment, -> { order(type: :desc) }

has_many :comments do
def find_most_recent
order("id DESC").first
Expand Down