Why does this work:
@poll_votes = PollVote.where(:user_id => self.user_id, :poll_id => self.poll_id).all
@poll_votes.each do |p|
p.destroy
end
But this does not?
@poll_votes = PollVote.where(:user_id => self.user_id, :poll_id => self.poll_id).destroy
Rails delete operation using destroy methodBy using destroy, you can delete the record from rails as well as its other existing dependencies. So in the context of our rails application, if we delete a book record using the destroy function, the authors associated with the book will also be deleted.
Any record where that column has a non- NULL value is considered to be soft-deleted. When a record is deleted via Active Record (the default ORM library packaged with Ruby on Rails), instead of actually deleting the record from the database, populate the deleted_at column with the time of deletion.
You can't explicitly destroy object. Ruby has automatic memory management. Objects no longer referenced from anywhere are automatically collected by the garbage collector built in the interpreter.
Dependent is an option of Rails collection association declaration to cascade the delete action. The :destroy is to cause the associated object to also be destroyed when its owner is destroyed.
The where method returns an enumerable collection of activerecord objects meeting the selection criteria. Calling the destroy method on that collection is different than calling the destroy method on a single activerecord object.
This should work: PollVote.destroy_all(:user_id => self.user_id, :poll_id => self.poll_id)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With