A new feature that myself and Mark Van Holstyn worked up was because we wanted an elegant solution to this in a problem we had. And it was time to finally put in the foreign key support that I hacked together over a year ago. Now it’s not a hack, and I think it’s kind of elegant.
# Disable foreign keys altogether ActiveRecord::Base.foreign_keys.disable # Enable foreign keys ActiveRecord::Base.foreign_keys.enable # Run some updates in a disable block and have AR automatically re-enable foreign keys Topic.foreign_keys.disable do # ... code here.... end # Run some updates in a enable block and have AR automatically re-disable foreign keys Topic.foreign_keys.enable do # ... code here .... end
This works on any instance of ActiveRecord::Base, and it works on the connection for that instance. So if you have models which are connected to different databases, no worries.
This is in trunk right now and works great for MySQL.
blog comments powered by Disqus