多态关联过程中创建有虚拟 imageable 表? ①②中都可以实现功能需求,项目开发中怎么都推荐使用①,多态关联性能高效?
①使用多态关联, model里面并没有imageable,数据库里也没有imageable表
class Picture < ApplicationRecord
belongs_to :imageable, polymorphic: true
end
class Employee < ApplicationRecord
has_many :pictures, as: :imageable
end
class Product < ApplicationRecord
has_many :pictures, as: :imageable
end
===========
②没有多态关联,看起来更加简洁
class Picture < ApplicationRecord
belongs_to :product
belongs_to :employ
end
class Employee < ApplicationRecord
has_many :pictures
end
class Product < ApplicationRecord
has_many :pictures
end