Created
January 7, 2011 12:54
-
-
Save solnic/769423 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/env ruby | |
| # | |
| # encoding: utf-8 | |
| require 'dm-sqlite-adapter' | |
| require 'dm-migrations' | |
| require 'dm-validations' | |
| DataMapper::Logger.new($stdout, :debug) | |
| DataMapper.setup :default, "sqlite::memory:" | |
| class Ban | |
| include DataMapper::Resource | |
| property :id, Serial | |
| property :name, String | |
| belongs_to :user # required by default | |
| end | |
| class User | |
| include DataMapper::Resource | |
| property :id, Serial | |
| property :name, String | |
| has n, :bans | |
| end | |
| DataMapper.finalize | |
| DataMapper.auto_migrate! | |
| john = User.create(:name => "john") | |
| jane = User.create(:name => "jane") | |
| b = Ban.create(:name => "foo", :user => john) | |
| b.user_id = jane.id | |
| b.name = "bar" | |
| puts b.user_id.inspect # it is 2 | |
| puts b.save # true | |
| puts b.name # *is* updated to 'bar' | |
| puts b.user_id.inspect # but this is again 1! | |
| puts b.user.inspect # so this is still john! | |
| puts b.errors.inspect # no errors |
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
| module DataMapper | |
| module Associations | |
| module ManyToOne | |
| class Relationship < Associations::Relationship | |
| # @api semipublic | |
| def get!(resource) | |
| parent = resource.instance_variable_get(instance_variable_name) | |
| if parent_key.get!(parent) != source_key.get!(resource) | |
| parent = resource_for(resource) | |
| end | |
| parent | |
| end | |
| end | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment