大家好,我有一个类似下面的爬虫模型的类:
class Link
include Mongoid::Document
include Mongoid::Timestamps
field :url, type: String
field :links, type: String
index :url
has_many :pages
end如果一个链接后悔了一个URL,并且他们有许多入站/出站连接,我希望它能正常工作,所以:
a_link.links # => gives a list of outbound link objects.你怎么用mongoid做到这一点?
发布于 2011-09-21 17:45:01
您可以在关系的每一端使用has_and_belongs_to_many设置多对多关联。
class Link
include Mongoid::Document
has_and_belongs_to_many :links, :class_name => 'Link', :inverse_of => :inbound_links
has_and_belongs_to_many :inbound_links, :class_name => 'Link', :inverse_of => :links
end在本例中,由于关联来往于同一个类,因此您需要在class_name和inverse_of方面为mongoid提供一点帮助,因为它不能从关联名称中推断出这一点。
发布于 2015-04-16 20:10:48
使用多对多关联进行归档的一种更简洁的方法
class Link
include Mongoid::Document
has_and_belongs_to_many :links, class_name: 'Link', inverse_of: :links
endhttps://stackoverflow.com/questions/7496847
复制相似问题