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 に答える