replicate icon indicating copy to clipboard operation
replicate copied to clipboard

handle cyclic associations

Open freng opened this issue 13 years ago • 4 comments

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:

  1. on dump: prevent following loops in the object association graph by saving and checking for objects already seen in the traversal
  2. 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

freng avatar Apr 03 '13 14:04 freng

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

rtomayko avatar Apr 03 '13 17:04 rtomayko

Any update on this? why not just giving a default nil value to local_id?

robink avatar Sep 25 '13 17:09 robink

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.

jjbohn avatar Jun 26 '14 21:06 jjbohn

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.

freng avatar Jan 07 '15 15:01 freng