ActiveRecord Foreign Key Nicety 24 Jan 2007
I’ve been busy lately and I haven’t finished the to_csv functionality for ActiveRecord::Extensions. It’s coming, but I haven’t had time to implement all of the tests.

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

# Enable foreign keys 

# Run some updates in a disable block and have AR automatically re-enable foreign keys
Topic.foreign_keys.disable do
   # ... code here....

# Run some updates in a enable block and have AR automatically re-disable foreign keys
Topic.foreign_keys.enable do
   # ... code here ....

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