我有这些模型
class State
include Mongoid::Document
field :name
embeds_many :cities
end
class City
include Mongoid::Document
field :name
field :zip_code
embedded_in :state
references_many :organization_addresses
end
class Organization
include Mongoid::Document
field :name
references_many :organization_addresses
end
class OrganizationAddress
include Mongoid::Document
field :address
field :latitude, :type=>Float
field :longitude, :type=>Float
referenced_in :organization
referenced_in :city
end现在,我想为一个组织创建一个地址,并将该地址引用到zip_code为06511的城市。首先,我如何找到嵌入在州中的城市。然后我如何引用它。我写了查询State.where("cities.zip_code"=>"06511").count,它不会返回任何东西,因为城市在数据库中。
如何搜索它,然后将其引用到组织的地址?
发布于 2011-10-04 20:26:08
这里的解决方案是两个将城市的id-s保存在State中,然后您可以找到在该数组中具有特定id-s的州。您可以使用IN、NIN (not in)等关键字在其中实现复杂的逻辑。
我真的推荐你去看看this来了解它。
发布于 2011-10-04 19:25:31
您不能在MongoID中引用嵌入文档。
https://stackoverflow.com/questions/7646821
复制相似问题