Skip to content

Instantly share code, notes, and snippets.

@JonKernPA
Created July 5, 2012 02:09
Show Gist options
  • Save JonKernPA/3050591 to your computer and use it in GitHub Desktop.
Save JonKernPA/3050591 to your computer and use it in GitHub Desktop.
Deleting an embedded document
class Period
include MongoMapper::EmbeddedDocument
key :text, String
embedded_in :schedule
def to_s
text
end
end
class Schedule
include MongoMapper::Document
key :name, String
many :periods
def to_s
text = "#{name}, periods: " + periods.join(', ')
end
end
s1 = Schedule.create(:name => "Thursday",
:periods => [
Period.new(:text => "Morning"),
Period.new(:text => "Afternoon"),
Period.new(:text => "Evening")
])
puts s1
#Thursday, periods: Morning, Afternoon, Evening
s1.periods.delete_if{|p| p.text == 'Afternoon'}
s1.save
puts s1
# Thursday, periods: Morning, Evening
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment