handle cyclic associations
cyclic associations lead to an infinite loop on dump, for instance
class Order < ActiveRecord::Base
has_many :states
belongs_to :last_state, :class_name => 'State'
end
class State < ActiveRecord::Base
belongs_to :order
end
I suggest two changes which I have implemented as a PoC:
- on dump: prevent following loops in the object association graph by saving and checking for objects already seen in the traversal
- on load: when a referenced object is missing, put the referrer on a wait list and reload it when the referee is loaded - otherwise there would be missing foreign keys
Thanks for this. I've run into the problem a few times now.
I need to do a bit closer review of the diff and approach but I'm definitely interested in landing something that fixes this.
/cc @jbarnette
Any update on this? why not just giving a default nil value to local_id?
Anything new on this? I just cut a new fork where I've merged this into it, but would prefer to track the gem from the source.
Trying to revive this, merged the rtomayko's current master branch and made two changes:
- for backward compat when load_replicant overriden: check in load method the arity of the load_replicant method, if it's not -4, then we cannot reload (i.e. update the already loaded object when dependent foreign keys become available) since method is overriden, no more ArgumentError (of course there will be missing foreign keys then)
- use of a wait list now instead of single waiting element since multiple objects could wait for the same dependent object
It would be nice if you could test dump & restore in (non-critical ,-) environments of your real world applications.