-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Respect new records for CollectionProxy#uniq
#26980
Respect new records for CollectionProxy#uniq
#26980
Conversation
r? @sgrif (@rails-bot has picked a reviewer for you, use r? to override) |
CollectionProxy#dictinct
CollectionProxy#distinct
I would have expected this code to just do |
This method exists since the initial commit. rails/activerecord/lib/active_record/associations/association_collection.rb Lines 72 to 74 in db045db
If we expects |
Currently if `CollectionProxy` has more than one new record, `CollectionProxy#uniq` result is incorrect. And `CollectionProxy#uniq` was aliased to `distinct` in a1bb6c8. But the `uniq` method and the `SELECT DISTINCT` method are different methods. The doc in `CollectionProxy` is for the `SELECT DISTINCT` method, not for the `uniq` method. Therefore, reverting the alias in `CollectionProxy` to fix the inconsistency and to have the both methods.
68ff93d
to
0ec967a
Compare
CollectionProxy#distinct
CollectionProxy#uniq
It would be useful to backport this to 5.0.x. Here's my failing test and the # Note that "good" and "bad" are reversed, because `git bisect` is only built to work in one direction.
# I use the trick shown here: http://stackoverflow.com/a/36157747/2651774 to get around that.
# git bisect start
# git bisect bad master
# git bisect good 5-0-stable^
# git bisect skip
# git bisect run bash -c "! ruby test.rb"
# fixed commit: 0ec967aa6655d62c92f72acb8d556a5b3f70762d
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
source "https://rubygems.org"
gem "rails", path: "."
gem "sqlite3"
end
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, force: true do |t|
end
create_table :comments, force: true do |t|
t.integer :post_id
t.string :comment
end
end
class Post < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post
end
class BugTest < Minitest::Test
def test_association_stuff
post = Post.create!
post.comments << Comment.create!(comment: 'test')
post.comments << Comment.create!(comment: 'test')
assert_equal ['test', 'test'], Post.last.comments.pluck(:comment)
assert_equal ['test'], Post.last.comments.distinct.pluck(:comment)
end
end |
Currently if
CollectionProxy
has more than one new record,CollectionProxy#uniq
result is incorrect.And
CollectionProxy#uniq
was aliased todistinct
in a1bb6c8.But the
uniq
method and theSELECT DISTINCT
method are differentmethods. The doc in
CollectionProxy
is for theSELECT DISTINCT
method, not for the
uniq
method.Therefore, reverting the alias in
CollectionProxy
to fix theinconsistency and to have the both methods.