Created
July 5, 2012 02:09
-
-
Save JonKernPA/3050591 to your computer and use it in GitHub Desktop.
Deleting an embedded document
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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