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
require"bundler/inline"gemfile(true)dosource"https://rubygems.org"gem"activerecord","7.1.3.2"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:users,force: :cascadedo |t|
endcreate_table:posts,force: :cascadedo |t|
t.integer:person_idendendclassUser < ActiveRecord::Basehas_many:posts,inverse_of: :userendclassPost < ActiveRecord::Basealias_attribute:user_id,:person_idbelongs_to:userendclassBugTest < Minitest::Testdeftest_associationuser=User.create!assertPost.create(user: user)# SuccessassertPost.create(user_id: user.id)# Successassertuser.posts.create!# Failureendend
Expected behavior
Post.create(user: user) works fine. But when creating the associated record via user.posts.create! it breaks because the foreign key is not getting set properly.
Additionally, we cannot specify belongs_to :user, foreign_key: :user_id or has_many :posts, inverse_of: :user, foreign_key: :user_id. One of those has to at least set foreign_key: :person_id for this to work.
Actual behavior
The has_many association calls set_owner_attributes:
I'm happy to help implement a solution, but not sure exactly where it should live. I don't know if doing it in the foreign key lookup would be cheaper at all, but it still requires a call to <record_class>.attribute_aliases.
System configuration
Rails version: 7.1.3.2
Ruby version: 3.1.0
The text was updated successfully, but these errors were encountered:
Steps to reproduce
Expected behavior
Post.create(user: user)
works fine. But when creating the associated record viauser.posts.create!
it breaks because the foreign key is not getting set properly.Additionally, we cannot specify
belongs_to :user, foreign_key: :user_id
orhas_many :posts, inverse_of: :user, foreign_key: :user_id
. One of those has to at least setforeign_key: :person_id
for this to work.Actual behavior
The
has_many
association callsset_owner_attributes
:rails/activerecord/lib/active_record/associations/has_many_association.rb
Line 62 in 6f0d1ad
Set owner attributes is called and gets the foreign key:
rails/activerecord/lib/active_record/associations/foreign_association.rb
Line 25 in 6f0d1ad
If it's not specified, the name is derived, in this case
user_id
when the has many checks the inversebelongs_to
:rails/activerecord/lib/active_record/reflection.rb
Lines 773 to 783 in 6f0d1ad
Then we call
_write_attribute
:rails/activerecord/lib/active_record/associations/foreign_association.rb
Line 32 in 6f0d1ad
This raises the error since
_write_attribute
does not reference aliases.write_attribute
would work:rails/activerecord/lib/active_record/attribute_methods/write.rb
Line 33 in 6f0d1ad
But @byroot mentions here a possible high cost to doing that.
I'm happy to help implement a solution, but not sure exactly where it should live. I don't know if doing it in the foreign key lookup would be cheaper at all, but it still requires a call to
<record_class>.attribute_aliases
.System configuration
Rails version: 7.1.3.2
Ruby version: 3.1.0
The text was updated successfully, but these errors were encountered: