3

I have the following model

class Professional
  include Mongoid::Document
  field :first_name, type: String
  field :last_name, type: String
  field :company_name, type: String
  field :address, type: String


  validates :first_name, length: { minimum: 5, :message => "What" }, format: { with: /\A[a-zA-Z]+\z/, message: "only allows letters" }
end

I want to include a embedded documents where i can store multiple office address. Am looking for the following Structure of the DB

{
  "first_name": "Harsha",
  "last_name": "MV",
  "company_name": "Mink7",
  "offices": [
    {
      "name": "Head Office",
      "address": "some address here"
    },
    {
      "name": "Off Site Office",
      "address": "some large address here"
    }
  ]
}
Harsha M V
  • 54,075
  • 125
  • 354
  • 529

1 Answers1

6

You will have to define that the model is embedding an office object and vice versa, explanation here: http://mongoid.org/en/mongoid/docs/relations.html. I'm guessing that you need a 1-N relation, so that a Professional can embed several offices? In that case, something like this should work.

Professional model

class Professional
  include Mongoid::Document
  field :first_name, type: String
  field :last_name, type: String
  field :company_name, type: String
  field :address, type: String


  validates :first_name, length: { minimum: 5, :message => "What" }, format: { with: /\A[a-zA-Z]+\z/, message: "only allows letters" }

  embeds_many :offices, class_name: "Office"
end

Office model

class Office
  include Mongoid::Document
  field :name, type: String
  field :address, type: String

  embedded_in :professional, :inverse_of => :offices
end

Remember that if you are going to use one form for these objects you'll have to do a nested form, something like (or just google something up):

<%= form_for @professional, :url => { :action => "create" } do |o| %>
    <%= o.text_field :first_name %>
    <%= o.text_field :last_name %>

    <%= o.fields_for :office do |builder| %>
        <%= builder.text_field :name %>
        <%= builder.text_field :address %>
    <% end %>
<% end %>

Note that nothing is tested.

zawhtut
  • 8,335
  • 5
  • 52
  • 76
oskarno
  • 126
  • 1
  • 2
  • 8
  • thanks a lot. if i want to add more than one offices can i add two set of fields and it will save? how can i add the two set of fields? – Harsha M V Jan 27 '14 at 17:21
  • 1
    Glad I could help. I'm not sure if that is going to work, haven't got the time to try it out either.. But you can just do the trial and error on that one, you already have the code anyway.. =) – oskarno Jan 28 '14 at 23:15
  • Maybe you can share your controller too, this should have office empty thus not displaying name and address for office. – Dennis May 26 '17 at 14:28