Skip to content

Adding Table Context to Order Hash #12610

Open
wants to merge 1 commit into from

4 participants

@prpetten

This commit fixes #12604

Prepend original table name to order hash

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]
paulp@devicemagic.com Adding Table Context to Order Hash
This commit fixes #12604

Prepend original table name to order hash

    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]
03e8964
@prpetten

This is my first pull request into rails core, and I'd be happy to hear any feedback.

NB: Apologies the previous pull request isn't showing up as my github account, although that email should be associated with my account.

@rafaelfranca rafaelfranca commented on the diff Oct 23, 2013
activerecord/test/models/post.rb
@@ -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) }
@rafaelfranca
Ruby on Rails member
rafaelfranca added a note Oct 23, 2013

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

@prpetten
prpetten added a note Oct 23, 2013

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

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

@rafaelfranca
Ruby on Rails member
rafaelfranca added a note Oct 23, 2013

Yes. this is what I'm meaning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@pftg pftg commented on the diff Oct 28, 2013
activerecord/CHANGELOG.md
@@ -862,4 +862,34 @@
*Slava Markevich*
+* Prepend original table name to order hash
@pftg
pftg added a note Oct 28, 2013

Please move this changelog entry to the first line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@pftg
pftg commented Oct 28, 2013

👍 also, should table name auto-added to references? Please review: https://github.com/prpetten/rails/blob/03e8964c7e0d1c370e924cc93e1fb73c3158ee9b/activerecord/lib/active_record/relation/query_methods.rb#L1036

And one moment is not clear for me: why you are expecting only one field?

@prpetten
@vipulnsward
Ruby on Rails member

@prpetten can you check failed build at https://travis-ci.org/rails/rails/jobs/12910131? Thanks.

@rafaelfranca rafaelfranca modified the milestone: 4.0.5, 4.0.4, 4.0.6 Mar 10, 2014
@rafaelfranca rafaelfranca modified the milestone: 4.0.10, 4.1.7 Aug 21, 2014
@rafaelfranca rafaelfranca modified the milestone: 4.1.9, 4.0.13 Nov 19, 2014
@rafaelfranca rafaelfranca modified the milestone: 4.0.13, 4.1.9 Jan 2, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.