在 Rails 4 中同时使用 POLYMORPHIC 和 :THROUGH 关联?

Using POLYMORPHIC and :THROUGH association at the same time in Rails 4?

我在 Rails 中遇到了更高级的关联问题。

这是我想要做的:

这是我认为应该如何布局的

class User < ActiveRecord::Base
  belongs_to :tenant, polymorphic: true
end

class Property < ActiveRecord::Base
  has_many :users, as: :tenant
  belongs_to :landlords

end

class Landlord < ActiveRecord::Base
  has_many :property
  has_many :users, as: :tenant, through: :properties

end

这是正确的吗?

是不是better/less没有多态关联就绕了,直接用through关联?

我会这样做:

class Landlord 
  has_many :properties
  has_many :tenants, through: :properties
end 

class Tenant
  belongs_to :property
  belongs_to :landlord, through: :properties
end

class Property
  belongs_to :landlord
  has_many :tenants
end 

查看此active record associations guide了解更多信息。