You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# frozen_string_literal: truerequire"bundler/inline"gemfile(true)dosource"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"endrequire"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.definedocreate_table:posts,force: truedo |t|
endcreate_table:comments,force: truedo |t|
t.integer:post_idt.boolean:visible,default: falseendendclassPost < ActiveRecord::Basehas_many:commentsendclassComment < ActiveRecord::Basebelongs_to:postdefault_scope->{where(visible: true)}endclassBugTest < Minitest::Testdeftest_association_stuffpost=Post.create!post.comments << Comment.create!new_comment=post.comments.unscope(where: :visible).newassert_equalfalse,new_comment.visibleendend
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
The text was updated successfully, but these errors were encountered:
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.
Fixesrails#37138.
When initializing a new AR object from an
unscoped
association, theunscope
is not taken into account.See
steps to reproduce
for an exampleRegression 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 behaviorSteps to reproduce
Expected behavior
in
new_comment = post.comments.unscope(where: :visible).new
, since the default scope is unscoped, I expectnew_comment.visible
to be false, since it's the default database value for the fieldActual behavior
in
new_comment = post.comments.unscope(where: :visible).new
, the unscope is ignored andnew_comment.visible
is true (due todefault_scope
)System configuration
Rails version: 6.0.0
Ruby version: 2.6.3
The text was updated successfully, but these errors were encountered: