您可以显式地这样做
class_name
在…上
belongs_to
和
has_many
foreign_key
在…上
你有很多
class Vendor < ApplicationModel
belongs_to :registered_address, class_name: 'Address'
belongs_to :billing_address, class_name: 'Address'
belongs_to :display_address, class_name: 'Address'
end
class Address < ApplicationModel
has_many :registered_addresses, foreign_key: :registered_address_id, class_name: 'Vendor'
has_many :billing_addresses, foreign_key: :billing_address_id, class_name: 'Vendor'
has_many :display_addresses, foreign_key: :billing_address_id, class_name: 'Vendor'
end
参考资料:-
has_many
belongs_to
查询=>
vendor_object.registered_address
vendor_object.billing_address
vendor_object.display_address