Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoID, embedding a document in multiple documents

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.

like image 332
rubish Avatar asked Nov 15 '22 01:11

rubish


1 Answers

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

like image 96
Julian Maicher Avatar answered Dec 21 '22 15:12

Julian Maicher