您可以使用 STI 方法(单继承表):
模型用户:
class User < ActiveRecord::Base
# general attributes and validations and the like
end
车主型号:
class Owner < User
# specific attributes and/or validations if any
has_many :products
end
借款人模式:
class Borrower < User
# specific attributes and/or validations if any
haas_many :products
end
产品型号:
class Product < ActiveRecord::Base
# attributes, validation and the like
belongs_to :owner
belongs_to :borrower
end
基本上,将 Owner 和 Borrower 组织为 User 类型,继承其属性。
one_owner.products
将向您展示 one_owner 拥有的产品
one_borrower.products
将向您展示 one_borrower 借用的产品
one_product.owner
将向您显示该产品的所有者
one_product.borrower
将向您显示该产品的借款人
您可以在此线程中看到一个广泛的示例:Rails Question: belongs_to with STI - 我如何正确地做到这一点?