public
Created

MongoMapper Associations

  • Download Gist
mongo_test.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.