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

has_many association: select with block given doesn't return newly built records #28348

Closed
dkamenka opened this issue Mar 8, 2017 · 0 comments

Comments

@dkamenka
Copy link

dkamenka commented Mar 8, 2017

Select with block given for has_many association is broken in 5.1.0.beta1 and current master. It doesn't include newly built records. It looks like the regression is caused by #25877.

Steps to reproduce

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", github: "rails/rails"
  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
  end
end

class Post < ActiveRecord::Base
  has_many :comments
end

class Comment < ActiveRecord::Base
  belongs_to :post
end

class BugTest < Minitest::Test
  def test_select
    post = Post.create!
    post.comments.build
    post.comments.build

    assert_equal 2, post.comments.size
    assert_equal 2, post.comments.select{ true }.size
  end

  def test_select_with_existing_item
    post = Post.create!
    post.comments.create!
    post.comments.build
    post.comments.build

    assert_equal 3, post.comments.size
    assert_equal 3, post.comments.select{ true }.size
  end
end

Expected behavior

Select with block given should select both newly built and existing items.

Actual behavior

Select returns only saved items.

System configuration

Rails version: 5.1.0.beta1

Ruby version: 2.4.0

kamipo added a commit to kamipo/rails that referenced this issue Mar 9, 2017
…association

The `select` in `QueryMethods` is also an enumerable method.
Enumerable methods with block should delegate to `records` on
`CollectionProxy`, not `scope`.

Fixes rails#28348.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants