it’s quite looklike with how to query child objects in mongodb
I Have:

Pais (Country) with its children (ufds(27) on total), I’m making a Ruby seed.rb file to bulky insert from a file.
the mapping files are:
class Pais
include Mongoid::Document
field :nome, :type => String
field :sigla, :type => String
embeds_many :ufds
validates_uniqueness_of :sigla
end
class Ufd
include Mongoid::Document
field :codigo_ibge, :type => String
field :sigla, :type => String
field :nome, :type => String
embedded_in :pais, :inverse_of => :ufds
embeds_many :cidades
validates_uniqueness_of :codigo_ibge, :sigla
end
class Cidade
include Mongoid::Document
field :codigo_ibge, :type => String
field :nome, :type => String
embedded_in :ufd, :inverse_of => :cidades
validates_uniqueness_of :codigo_ibge
end
So when importing, I do beside other things the following:
pais_base = Pais.create!(:nome => "Brasil", :sigla => "BR")
File.open(caminho + 'estados.txt').each_with_index do |linha, index|
sigla, nome, ibge = linha.chomp.split("|")
pais_base.ufds << Ufd.new(:sigla => sigla, :nome => nome, :codigo_ibge => ibge )
end
which creates correctly the PAIS and its UFDS children, but now to create a children of UFDS, I load another file and try to find a UFDS with id (codigo_ibge), but always returns null
File.open(caminho + 'cidades.txt').each_with_index do |linha, index|
ufd, ibge, nome = linha.chomp.split("|")
uf = pais_base.ufds.find(:first, :conditions => {:codigo_ibge => ufd.to_s }) <<<<< NIL
uf.cidades << Cidade.new(:codigo_ibge => ibge.to_s, :nome => nome)
end
How should I do that? I’ve run out of ideas :/
Thanks in advance.
What version of mongoid are you using?
I think your best bet is to use where
This would make your query
find is only really used when you are looking up an id.