gpt4 book ai didi

ruby-on-rails-3 - MongoID,在多个文档中嵌入一个文档

转载 作者:行者123 更新时间:2023-12-04 07:12:40 24 4
gpt4 key购买 nike

我有一个模型地址如下

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

有嵌入地址的模型 College 和 User
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

更新:

我升级到 mongoid 2.0.1 并更改了我的用户以在地址中包含选项的倒数。
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),您应该将其用于逆的。

最佳答案

在我看来很好。我有一个类似的设置,它按预期工作。

关于ruby-on-rails-3 - MongoID,在多个文档中嵌入一个文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5349432/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com