Created
February 25, 2011 20:54
-
-
Save usergenic/844466 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #!/usr/bin/ruby | |
| require 'rubygems' | |
| require 'data_objects' # current master @ ed1ceefc | |
| require 'dm-core' # current master @ 73860575 | |
| require 'dm-migrations' # current master @ ab608e81 | |
| class Order | |
| include ::DataMapper::Resource | |
| property :id, Serial | |
| has 0..n, :order_items | |
| end | |
| class OrderItem | |
| include ::DataMapper::Resource | |
| property :id, Serial | |
| belongs_to :order | |
| end | |
| ::DataMapper.setup(:default, 'mysql://127.0.0.1/test') | |
| ::DataMapper.auto_migrate! | |
| o1 = Order.create | |
| o2 = Order.create | |
| i1 = OrderItem.create :order_id => o1.id | |
| i1 = o1.order_items.first | |
| puts "assigning i1's order_id to 2..." | |
| i1.order_id = 2 | |
| i1.save | |
| puts "we just saved i1 and i1.clean? is #{i1.clean?}" | |
| puts "lets reload i1..." | |
| i1.reload # reload attributes from DB but ALSO divorces/unbinds from the association/parent? | |
| puts "i1.order_id should be 2 but it is #{i1.order_id}" | |
| puts "lets assign i1's order_id to 2 again..." | |
| i1.order_id = 2 | |
| i1.save | |
| puts "we just saved i1 and i1.clean? is #{i1.clean?}" | |
| puts "lets reload i1" | |
| i1.reload | |
| puts "i1.order_id should be 2 and now it is #{i1.order_id}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment