MongoID,将一个文档嵌入到多个文档中

发布于 2024-10-24 22:38:27 字数 2716 浏览 0 评论 0原文

我有一个如下所示的模型地址

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

更新:

我升级到 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),则应该将其用于逆的。

I have a model Address like following

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

There are models College and User which embed address

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

I am getting some problems with the above setup. I am using single form to ask for current and permanent address along with some more information, but only current_address is getting saved and that too with the data I populate in 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')}}})

I am not sure if this is something I am doing wrong here or there is some other problem. I am using Rails 3.0.4 and MongoID 2.0.0.rc.7

Update:

I upgraded to mongoid 2.0.1 and changed my user to include inverse of options in address.

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

I know the inverse of names doesn't make sense, but the main point here is just to make them different or if you have good names for relations in your embedded class(like :current_user, :permanent_user), you should use that for inverse of.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我喜欢麦丽素 2024-10-31 22:38:27

我觉得不错。我有类似的设置并且它按预期工作。

Looks good to me. I've a similar setup and it works as expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文