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

发布于 2024-12-01 13:29:13 字数 1148 浏览 1 评论 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?”询问。

帮助!

Rails 2.3.11 application

Mongoid 1.9.2 (latest "legacy" branch)

Since there's no documentation for Mongoid 1.X branch anymore, I'm struggling with what I've got
configured wrong in this example. It seems I'm not querying an embedded document correctly. How
should I be doing it instead?

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

At the console, I type

a = GraphPage.new(page_data_1)
a.add_relationship(link1)

And it returns

Error : NoMethodError: undefined method `where' for BSON::OrderedHash:0x00000114c1e8e0 

with the error being the "has_link?" query.

Help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝颜夕 2024-12-08 13:29:13

我猜你不能这样编写查询。也许尝试一下

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

I'm going to guess that you can't compose queries like that. Perhaps try

def has_link?(url)
  graph_links.any? { |doc| doc.url == url }
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文