Outline

You should remove child node if parent node is delete in mongodb. Here is simple example.

Models

Parent

class Parent
  include Mongoid::Document

  field :name,  type: String
  has_many :children
end

Child

class Child
  include Mongoid::Document

  field :name,  type: String
  belongs_to :parent
end

Example

Bad

$ rails c
$ parent = Parent.create(name: "parent1")
$ parent.children.create(name: "child1")
$ child = parent.children.first

# if you delete parent child1 still exist in db.
$ parent.destroy
$ Parent.where(id, parent.id).size # result is 0
$ Child.where(id, child.id).size   # result is 1

Solution

You have to add cleanup logic using “before_destroy” callback

Parent

class Parent
  include Mongoid::Document

  field :name,  type: String
  has_many :children

  before_destroy do
    children.destroy_all
  end
end

Example

Good

$ rails c
$ parent = Parent.create(name: "parent1")
$ parent.children.create(name: "child1")
$ child = parent.children.first

# if you delete parent child1 is delete too in db.
$ parent.destroy
$ Parent.where(id, parent.id).size # result is 0
$ Child.where(id, child.id).size   # result is 0

add ticket number to git commit automatically

Most of ticket tracker like Github, pivotal tracker have function to connect your commit to ticket(story).But every time when you commit ...… Continue reading

I will build frontend and backend separately

Published on September 28, 2016

order of angular controller's initialisation

Published on October 05, 2015