Skip to content

Instantly share code, notes, and snippets.

@JKring
Created July 13, 2012 00:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JKring/3101910 to your computer and use it in GitHub Desktop.
Save JKring/3101910 to your computer and use it in GitHub Desktop.
MongoMapper Associations
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