由于Mongoid 1.X分支机构已经没有文档了,我正在努力解决我所拥有的问题 在此示例中配置错误。好像我没有正确查询嵌入式文档。怎么样 我应该这样做吗?
class GraphLink
include Mongoid::Document
embedded_in :graph_pages, :inverse_of => :graph_links
end
class GraphInlink
include Mongoid::Document
embedded_in :graph_pages, :inverse_of => :graph_inlinks
end
class GraphPage
include Mongoid::Document
embeds_many :graph_links
embeds_many :graph_inlinks
def add_relationship(link)
unless has_link?(url)
self.graph_links << GraphLink.new(link)
destination_page = GraphPage.where(:url => link[:url]).first
destination_page.graph_links << GraphInlinks.new(link)
destination_page.save
self.save
end
end
def has_link?(url)
graph_links.where(:url => url).count > 0
end
end
在控制台,我输入
a = GraphPage.new(page_data_1)
a.add_relationship(link1)
它返回
Error : NoMethodError: undefined method `where' for BSON::OrderedHash:0x00000114c1e8e0
错误是“has_link?”查询。
帮助!
答案 0 :(得分:0)
我猜你不能写那样的查询。也许试试
def has_link?(url)
graph_links.any? { |doc| doc.url == url }
end