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

AR: new instance of unscoped association ignores unscope #37138

Closed
norydev opened this issue Sep 5, 2019 · 0 comments · Fixed by #37511
Closed

AR: new instance of unscoped association ignores unscope #37138

norydev opened this issue Sep 5, 2019 · 0 comments · Fixed by #37511
Assignees

Comments

@norydev
Copy link

norydev commented Sep 5, 2019

When initializing a new AR object from an unscoped association, the unscope is not taken into account.

See steps to reproduce for an example

Regression Introduced by 17f2f30, it used to work on Rails 5-2

here exactly: https://github.com/rails/rails/blob/6-0-stable/activerecord/lib/active_record/associations/association.rb#L228 reverting to klass.all induces the expected behavior

Steps to reproduce

# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  # Activate the gem you are reporting the issue against.
  gem "activerecord", "6.0.0"
  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.boolean :visible, default: false
  end
end

class Post < ActiveRecord::Base
  has_many :comments
end

class Comment < ActiveRecord::Base
  belongs_to :post
  default_scope -> { where(visible: true) }
end

class BugTest < Minitest::Test
  def test_association_stuff
    post = Post.create!
    post.comments << Comment.create!

    new_comment = post.comments.unscope(where: :visible).new

    assert_equal false, new_comment.visible
  end
end

Expected behavior

in new_comment = post.comments.unscope(where: :visible).new, since the default scope is unscoped, I expect new_comment.visible to be false, since it's the default database value for the field

Actual behavior

in new_comment = post.comments.unscope(where: :visible).new, the unscope is ignored and new_comment.visible is true (due to default_scope)

System configuration

Rails version: 6.0.0

Ruby version: 2.6.3

@kamipo kamipo self-assigned this Sep 5, 2019
kamipo added a commit to kamipo/rails that referenced this issue Oct 19, 2019
The intent of rails#35868 is to make the association loading consistently, it
should not have any side-effect for new instance creation on association
relation.

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

Successfully merging a pull request may close this issue.

2 participants