Mongoid 1.9.2 + Rails 2.3:BSON :: OrderedHash返回错误而不是对象

时间:2011-08-27 02:27:26

标签: ruby-on-rails mongoid bson

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?”查询。

帮助!

1 个答案:

答案 0 :(得分:0)

我猜你不能写那样的查询。也许试试

def has_link?(url)
  graph_links.any? { |doc| doc.url == url }
end