MongoID,嵌入在多个文档
问题描述:
我有一个模型地址的文件像下面MongoID,嵌入在多个文档
class Address
include Mongoid::Document
field :line1
field :city
# more fields like this
embedded_in :user, :inverse_of => :permanent_address
embedded_in :user, :inverse_of => :current_address
embedded_in :college, :inverse_of => :address
end
有机型学院和用户其嵌入地址
class College
include Mongoid::Document
references_many :users
embeds_one :address
# some fields and more code
end
class User
include Mongoid::Document
referenced_in :college, :inverse_of => :users
embeds_one :permanent_address, :class_name => "Address"
embeds_one :current_address, :class_name => "Address"
# fields and more code
end
我得到的一些问题与上面的设置。我使用单一表单来询问当前和永久地址以及一些更多信息,但只有current_address正在保存,而且我也将数据填充到permanent_address中。
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"KdOLvzmKyX341SSTc1SoUG6QIP9NplbAwkQkcx8cgdk=",
"user"=> {
"personal_info_attributes"=>{...},
"nick_names_attributes"=>{...},
"current_address_attributes"=>{
"line1"=>"",
"area"=>"",
"country"=>"USA",
"postal_code"=>"sd",
"city"=>"",
"state"=>"",
"landmark"=>"",
"id"=>"4d891397932ecf36a4000064"
},
"permanent_address_attributes"=>{
"line1"=>"",
"area"=>"asd",
"country"=>"india",
"postal_code"=>"",
"city"=>"",
"state"=>"",
"landmark"=>""
},
"commit"=>"Submit", "id"=>"4d8903d6932ecf32cf000001"}
MONGODB alma_connect['users'].find({:_id=>BSON::ObjectId('4d8903d6932ecf32cf000001')})
MONGODB alma_connect['users'].update({"_id"=>BSON::ObjectId('4d8903d6932ecf32cf000001')},
{"$set"=>{
"current_address"=>{
"line1"=>"",
"area"=>"asd",
"country"=>"india",
"postal_code"=>"",
"city"=>"",
"state"=>"",
"landmark"=>"",
"_id"=>BSON::ObjectId('4d8916e9932ecf381f000005')}}})
我不确定这是我做错了还是还有其他一些问题。我使用Rails 3.0.4和MongoID 2.0.0.rc.7
更新:
我升级到2.0.1 mongoid和改变了我的用户包括在地址的选择相反。
class User
include Mongoid::Document
referenced_in :college, :inverse_of => :users
embeds_one :permanent_address, :class_name => "Address", :inverse_of => :permanent_address
embeds_one :current_address, :class_name => "Address", :inverse_of => :current_address
# fields and more code
end
我知道名字的倒数就没有意义了,但这里的要点就是使他们不同,或者如果您有好的名字在嵌入式阶级关系(如:CURRENT_USER,:permanent_user) ,你应该使用它来反转。
答
对我很好。我有一个类似的设置,并按预期工作。
答
从Mongoid docs
当一个孩子嵌入文档可以属于不止一种类型的主文件,你可以告诉Mongoid通过将作为选项对父母的定义,以支持这一点,和多态对孩子的选择。在子对象上,将存储指示父类型的附加字段。
class Band
include Mongoid::Document
embeds_many :photos, as: :photographic
has_one :address, as: :addressable
end
class Photo
include Mongoid::Document
embedded_in :photographic, polymorphic: true
end
class Address
include Mongoid::Document
belongs_to :addressable, polymorphic: true
end
感谢您的确认 – rubish 2011-03-19 14:04:54
我正面临着上述问题。我使用单个表单来获取current_address和permanent_address,但只有当前地址正在保存。 – rubish 2011-03-22 21:42:23