3

在我的 Rails 应用程序中,我有两个模型: PersonCompany. 我需要指定这些对象的任何对之间的多对多关系。所以我应该可以这样做:

@connections = @person.connections

其中@connectionsPersonCompany对象的数组。

现在我已经ConnectionBinding为此创建了模型,但它不能按预期工作:

class ConnectionBinding < ActiveRecord::Base
  belongs_to :connect_from, polymorphic: true
  belongs_to :connect_to,   polymorphic: true
end

它认为ActiveRecord::HasManyThroughAssociationPolymorphicSourceError例外

有人已经解决了这个问题吗?任何建议表示赞赏。

4

3 回答 3

7

您需要告诉 ActiveRecord 它正在寻找的关联列。我猜你想要以下内容:

class Person < ActiveRecord::Base
  has_many :connection_bindings
  has_many :companies, :through => :connection_bindings
  has_many :people, :through => :connection_bindings
end

class company < ActiveRecord::Base
  has_many :connection_bindings
  has_many :companies, :through => :connection_bindings
  has_many :people, :through => :connection_bindings
end

问题是您有两个表,将 id 放在一列中,Rails 不知道要查找哪个表。

例如,在数据库中任何给定的 connection_binding 行上,connect_from 可以是 company_id 或 person_id,connect_to 也是如此。所以你说:'Hey Rails,加载我关联的 ConnectionBindings',它得到一行 connect_from 为 11,connect_to 为 12。但它是 Person.find(12) 还是 Company.find(12)?没有办法说!

相反,您必须向 Rails 提供更多信息:

class Person < ActiveRecord::Base
  has_many :connection_bindings
  has_many :person_connections, :through => :connection_bindings, :source => :to_connect, :source_type => 'Person'
  has_many :company_connections, :through => :connection_bindings, :source => :to_connect, :source_type => 'Company

  def connections
    person_connections + company_connections
  end
end

您需要在另一端构建它(以及相关的 :from_connect),但这取决于您如何使用这些连接。应该足以让你开始。

它比您在 Ruby 和 Rails 的神奇世界中所习惯的要多得多,但您正在尝试构建一个非常复杂的数据模式。这并不意味着它是不可能的——Rails,作为一个好的框架,不会阻止你做任何你真正想做的事情——但它并不常见到需要一些明确的结果。

于 2013-03-12T17:03:13.443 回答
2

感谢克里斯展示这个想法。但我需要做一些改变才能完成这项工作。Chris 示例的问题是,如果我调用person_connections方法,它会生成错误的查询。

Person.find(720).person_connections

生成此 SQL

SELECT  "people".* FROM "people"
INNER JOIN "connection_bindings"
  ON "people"."id" = "connection_bindings"."connect_to_id"
WHERE "connection_bindings"."person_id" = 720
  AND "connection_bindings"."connect_to_type" = 'Person'

那一person_id栏应该是connect_from_id。所以我需要添加:as => :connect_from选项来has_many :connection_bindings显示 ActiveRecord 它是一个多态关联。

has_many :connection_bindings, :as => :connect_from
has_many :person_connections,  :through => :connection_bindings,
  :source => :connect_to, source_type: 'Person'
has_many :company_connections, :through => :connection_bindings,
  :source => :connect_to, source_type: 'Company'

但这仍然不够。我需要能够获得反向​​连接。那么如果 Person@a将连接添加到 Person @b?方法连接应该在两个方向@a.connections@b.connections.

现在我想我可以通过添加一些额外的关联和聚合方法来做到这一点。

于 2013-03-13T01:13:39.243 回答
0

关心:

module Linkable
  extend ActiveSupport::Concern

  included do
    has_many :links_to, as: :linkable_to, class_name: 'Link' # [this]->[other object]
    has_many :links_from, as: :linkable_from, class_name: 'Link' # [other object]->[this]
  end
end

链接表:

class Link < ActiveRecord::Base

  #The idea for this class is to by a double polymorphic table, linking an object to another object
  belongs_to :linkable_from, :polymorphic => true
  belongs_to :linkable_to, :polymorphic => true
end
于 2013-11-15T19:51:17.813 回答