S
S
Shaks2015-02-26 09:59:46
Ruby on Rails
Shaks, 2015-02-26 09:59:46

How to correctly delete records in related tables?

There is a model. The model has associations with other models. The option :dependent => :destroyis present.. deletion is in progress... but if we look at the console, we will see something like this: User
f48ef6ea9bd840949faacbbe293a3a2a.png
model

class User < ActiveRecord::Base
  has_many :transactions, :dependent => :destroy
end

Transaction Model
class Transaction < ActiveRecord::Base
  belongs_to :user
end

Actually, why is done SELECT * FROMand then there is a "piece" deletion of records from the table? Moreover, only id is used for deletion, and the selection takes everything.
I need to get around this "feature" and delete DELETE FROM `transactions` WHERE `user_id` = ?
it like a human being it was not critical. But now it’s critical and I don’t need such chaos)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Viktor Vsk, 2015-02-26
@shaks

dependent: delete_all
Lawlessness for the sake of all callbacks and validations working for you

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question