mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
5b4a516c1c
Fix: #35204. This PR added validation to `automatic_inverse_of` that foreign_keys are the same. If class has multiple `belongs_to` to same class, `automatic_inverse_of` can find the wrong `inverse_name`. ```ruby class Room < ActiveRecord::Base belongs_to :user belongs_to :owner, class_name: "User" end class User < ActiveRecord::Base has_one :room has_one :owned_room, class_name: "Room", foreign_key: "owner_id" end user = User.create! owned_room = Room.create!(owner: user) p user.room ``` The current `automatic_inverse_of` validates the `reflection` that found from associations. However, its validation does not validate that foreign keys are the same. so this issue can be fixed by adding a validation of foreign keys. Co-authored-by: alpaca-tc <alpaca-tc@alpaca.tc> Co-authored-by: cat2koban <taba.noritomo@moneyforward.co.jp> Co-authored-by: luccafort <konishi.tatsuro@moneyforward.co.jp>
22 lines
626 B
Ruby
22 lines
626 B
Ruby
# frozen_string_literal: true
|
|
|
|
require "models/job"
|
|
|
|
class User < ActiveRecord::Base
|
|
has_secure_token
|
|
has_secure_token :auth_token, length: 36
|
|
|
|
has_and_belongs_to_many :jobs_pool,
|
|
class_name: "Job",
|
|
join_table: "jobs_pool"
|
|
|
|
has_one :room
|
|
has_one :owned_room, class_name: "Room", foreign_key: "owner_id"
|
|
has_one :family_tree, -> { where(token: nil) }, foreign_key: "member_id"
|
|
has_one :family, through: :family_tree
|
|
has_many :family_members, through: :family, source: :members
|
|
end
|
|
|
|
class UserWithNotification < User
|
|
after_create -> { Notification.create! message: "A new user has been created." }
|
|
end
|