Skip to content

Instantly share code, notes, and snippets.

@dkubb
Created July 28, 2008 21:22
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 dkubb/2955 to your computer and use it in GitHub Desktop.
Save dkubb/2955 to your computer and use it in GitHub Desktop.
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