Mongoid 1.9.2 + Rails 2.3: Errors with BSON::OrderedHash being returned instead of object
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 Mong开发者_StackOverflowoid::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!
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
精彩评论