I'm new to rails and trying to figure out associations using Mongoid.
I have a Picture model that can have many colors, and one color can belong to many pictures. Queries should work in both directions (ie Picture -> Colors and Color -> Pictures), so I decided to go with has_many_and_belongs_to association.
Class Picture
include Mongoid::Document
has_many_and_belongs_to :colors
Now the Tag Model
Class Color
include Mongoid::Document
has_many_and_belongs_to :pictures
I'd like to set it up so that when all the pictures associated to a color are deleted, the color is also deleted. I tried dependent with destroy and delete but none of them seem to work.
p1 = Picture.new
p2 = Picture.new
c = Color.new
p1.colors.push(c)
p2.colors.push(c)
p1.delete # <-- c is still associated with p2. This should not delete c
p2.delete # <-- c has no more associations. It should automatically be deleted now
Can this be automatically handled by rails? If not, how can I write delete/destroy callbacks to make this happen?