Rails does not support referential integrity foreign keys. How do I manage this?
Ideally the app should not have to deal with this; the database should. Do plugins like Foreigner have shortcomings? How is this dealt with?
Rails does not support referential integrity foreign keys. How do I manage this?
Ideally the app should not have to deal with this; the database should. Do plugins like Foreigner have shortcomings? How is this dealt with?
It's a design decision for Rails ActiveRecord.
I consider stored procedures and constraints vile and reckless destroyers of coherence. No, Mr. Database, you can not have my business logic. Your procedural ambitions will bear no fruit and you'll have to pry that logic from my dead, cold object-oriented hands.
Choose a single layer of cleverness - DHH
So the answer is that referential integrity handled by the DB is not the Rails way at all.
If you are unfamiliar with referential integrity, it is the way for relational databases to prevent the accidental deletion of records that are pointed to by other records.
You can do this programmatically using rails referential integrity by adding the following to your has_one or has_many clause:
,:dependent => :restrict_with_error
If instead you want all of the children records to be destroyed with the parent record, use this on your has_one or has_many clause:
,:dependent => :destroy
Active record has many options for dealing with dependent records. see:
© 2022 - 2024 — McMap. All rights reserved.