Skip to content

Instantly share code, notes, and snippets.

@JKring
Created May 23, 2012 00:21
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/2772500 to your computer and use it in GitHub Desktop.
Save JKring/2772500 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 :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