Assuming I have two tables: users
and orders
. A user has many orders, so naturally there is a foreign key user_id in my orders table.
What is the best practice in rails (in terms of speed, style and referential integrity) to ensure that if a user is deleted, all dependent orders are also deleted? I am considering the following options:
Case 1. Using :dependent => :destroy
in the user model
Case 2. Defining the table orders in postgres and writing
user_id integer REFERENCES users(id) ON DELETE CASCADE
Is there any reason why I should use Case 1? It seems that Case 2 is doing all I want it to do? Is there are difference in terms of execution speed?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…