Mongoid 1.9.2 + Rails 2.3:返回 BSON::OrderedHash 而不是对象时出现错误
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜你不能这样编写查询。也许尝试一下
I'm going to guess that you can't compose queries like that. Perhaps try