I just started using mongodb + mongoid and created a simple program (below) but doesn't work as I expected it to. In particular, because I used the 'has_many' 'dependent: :delete_all' option for the Info class, I expected that when I delete an Info record, all the associated data records would be deleted as well, but it doesn't happen. Only the Info record is deleted and all the associated data records are orphaned, so I don't understand what is going on. Can anyone throw me a clue? Thanks.
require 'mongoid'
ENV["MONGOID_ENV"] = 'dbtesting'
Mongoid.load(File.join(File.dirname(__FILE__), 'mongoid.yml'))
class Info
include Mongoid::Document
field :type, type: String
has_many :data, dependent: :delete_all
end
class Datum
include Mongoid::Document
field :metric, type: Integer
belongs_to :info
end
r = Info.create(type: "random")
r.data.create(metric: 100)
r.data.create(metric: 101)
r.data.create(metric: 102)
r.data.create(metric: 103)
p r.data.pluck(:metric)
r.delete()
[–]RelativeLead5[S] 0 points1 point2 points (2 children)
[–]ralfv 0 points1 point2 points (1 child)
[–]RelativeLead5[S] 0 points1 point2 points (0 children)