Created
July 13, 2012 00:00
-
-
Save JKring/3101910 to your computer and use it in GitHub Desktop.
MongoMapper Associations
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
require 'rubygems' | |
require 'mongo_mapper' | |
MongoMapper.database = "test_associations" | |
class Job | |
include MongoMapper::Document | |
belongs_to :invoice | |
before_save :do_something | |
def do_something | |
puts "saving a job" | |
end | |
end | |
class Invoice | |
include MongoMapper::Document | |
many :jobs | |
before_save :do_something | |
def do_something | |
puts "saving an invoice" | |
end | |
end | |
invoice = Invoice.new | |
10.times{ invoice.jobs << Job.create } | |
invoice.save | |
puts "==== Now we've created an Invoice with 10 Jobs ====" | |
puts "==== If we touch that Invoice, no problemo ====" | |
invoice = Invoice.first | |
invoice.save | |
puts "==== But if we touch that Invoice, and then touch all its Jobs, we have to save them all ====" | |
invoice.jobs.collect | |
invoice.save |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment