Scope w/ order clause hash (field: :desc) loses original model in merge #12604

Closed
prpetten opened this Issue Oct 22, 2013 · 4 comments

Comments

Projects
None yet
5 participants
@prpetten

Set up a scope on model using the order hash method.

class Post < ActiveRecord::Base
  has_many :comments
  scope :recent, -> { order(published_at: :desc) }
  scope :longago, -> { order(:published_at) }
end

Then attempt to call that scope as part of a merge.

 Comment.where(user_id: 1).joins(:posts).merge(Post.recent)

returns:
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: comments.published_at: SELECT "comments".* FROM "comments" INNER JOIN "posts" ON "posts"."id" = "comments"."post_id" WHERE "comments"."user_id" = 1 ORDER BY "comments"."published_at" DESC

It should behave like the symbol method for order, which keeps the original table/model the scope was defined on when performing a merge.

unless File.exists?('Gemfile')
  File.write('Gemfile', <<-GEMFILE)
    source 'https://rubygems.org'
    gem 'rails', github: 'rails/rails'
    gem 'sqlite3'
  GEMFILE

  system 'bundle'
end

require 'bundler'
Bundler.setup(:default)

require 'active_record'
require 'minitest/autorun'
require 'logger'

# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :posts do |t|
    t.string  :subject
  end

  create_table :comments do |t|
    t.integer :post_id
    t.integer :points
  end
end

class Post < ActiveRecord::Base
  has_many :comments

  scope :dewey, -> { order(subject: :desc) }
  scope :truman, -> { order(:subject) }
end

class Comment < ActiveRecord::Base
  belongs_to :post

  scope :decimal, -> { order(points: :desc) }
  scope :dewey_decimal, -> { decimal.joins(:post).merge(Post.dewey) }
  scope :truman_decimal, -> { decimal.joins(:post).merge(Post.truman) }
end

class BugTest < Minitest::Test
  def test_association_stuff
    post1 = Post.create!(subject: "Awesomeness")
    comment1 = Comment.create!(post_id: post1.id, points: 5)
    comment2 = Comment.create!(post_id: post1.id, points: 9)
    post2 = Post.create!(subject: "Zaniness")
    comment3 = Comment.create!(post_id: post2.id, points: 9)
    comment4 = Comment.create!(post_id: post2.id, points: 3)

    assert_equal Comment.truman_decimal, [comment2, comment3, comment1, comment4]
    assert_equal Comment.dewey_decimal, [comment3, comment2, comment1, comment4]
  end
end

Results:
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: comments.subject: SELECT "comments".* FROM "comments" INNER JOIN "posts" ON "posts"."id" = "comments"."post_id" ORDER BY "comments"."points" DESC, "comments"."subject" DESC

prpetten pushed a commit to prpetten/rails that referenced this issue Oct 23, 2013

paulp@devicemagic.com 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]

@rafaelfranca rafaelfranca modified the milestones: 4.0.5, 4.0.4, 4.0.6 Mar 10, 2014

@rails-bot rails-bot added the stale label Aug 19, 2014

@rails-bot

This comment has been minimized.

Show comment Hide comment
@rails-bot

rails-bot Aug 19, 2014

This issue has been automatically marked as stale because it has not been commented on for at least
three months.

The resources of the Rails team are limited, and so we are asking for your help.

If you can still reproduce this error on the 4-1-stable, 4-0-stable branches or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

This issue has been automatically marked as stale because it has not been commented on for at least
three months.

The resources of the Rails team are limited, and so we are asking for your help.

If you can still reproduce this error on the 4-1-stable, 4-0-stable branches or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

@rafaelfranca rafaelfranca modified the milestones: 4.0.10, 4.1.7 Aug 21, 2014

@olivierlacan

This comment has been minimized.

Show comment Hide comment
@olivierlacan

olivierlacan Oct 14, 2014

Contributor

I was able to reproduce this issue on Rails 4.1.2.

Contributor

olivierlacan commented Oct 14, 2014

I was able to reproduce this issue on Rails 4.1.2.

@rafaelfranca

This comment has been minimized.

Show comment Hide comment
@rafaelfranca

rafaelfranca Oct 15, 2014

Owner

The newest version is 4.1.7 so please make sure you are testing against it.

Owner

rafaelfranca commented Oct 15, 2014

The newest version is 4.1.7 so please make sure you are testing against it.

@rafaelfranca rafaelfranca removed the stale label Oct 15, 2014

@rafaelfranca rafaelfranca modified the milestones: 4.1.9, 4.0.13 Nov 19, 2014

@rafaelfranca rafaelfranca modified the milestones: 4.0.13, 4.1.9 Jan 2, 2015

@alexcameron89

This comment has been minimized.

Show comment Hide comment
@alexcameron89

alexcameron89 May 2, 2017

Member

This issue is passing on master, so I'm going to close it. Fixed by f83c9b1.

Member

alexcameron89 commented May 2, 2017

This issue is passing on master, so I'm going to close it. Fixed by f83c9b1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment