Created
July 28, 2008 21:22
-
-
Save dkubb/2955 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
father = Father.new | |
child = Child.new(:father => father) | |
father.save | |
child.father_id.should == father.id # how? the child references the father, but there's no backlink atm | |
# ... unless | |
class Child | |
def father | |
return @father if @father | |
if @father_id | |
self.father = Father.get(@father_id) | |
end | |
@father | |
end | |
def father=(father) | |
@father = father | |
@father_id = nil # @father should be authoritative | |
end | |
def father_id | |
@father.id || @father_id | |
end | |
def father_id=(father_id) | |
return if @father && @father_id == @father.id # do nothing if no change | |
if @father && @father_id != @father.id | |
@father = nil | |
end | |
@father_id = father_id | |
end | |
# ... | |
end | |
# now changing the father object will update the father_id key |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment