Created
May 23, 2012 00:21
-
-
Save JKring/2772500 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 :hello_world | |
def hello_world | |
puts "saving a job" | |
end | |
end | |
class Invoice | |
include MongoMapper::Document | |
many :jobs | |
before_save :hello_world | |
def hello_world | |
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 all its Jobs, we have to save them all (even if we didn't modify any of them) ====" | |
invoice.jobs.collect | |
invoice.save | |
puts "==== At scale, this is pretty limiting ==== " |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment