0

Rails 2.3.11 应用

Mongoid 1.9.2(最新的“遗留”分支)

由于不再有 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?” 询问。

帮助!

4

1 回答 1

0

我猜你不能像那样编写查询。也许尝试

def has_link?(url)
  graph_links.any? { |doc| doc.url == url }
end
于 2011-08-27T03:58:50.587 回答