Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I destroy a record without an ID column in ruby ActiveRecord?

Tags:

People also ask

How do you delete an Active Record?

Deleting a row from a particular table or a record set from a table is pretty simple, from your console all you need to do is grab the record set by its Id then delete or destroy it.

How do I delete a record in Ruby on Rails?

Rails delete operation using destroy method By 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.

What is the difference between delete and destroy in rails?

Basically destroy runs any callbacks on the model while delete doesn't. Deletes the record in the database and freezes this instance to reflect that no changes should be made (since they can't be persisted). Returns the frozen instance.

How do I delete a record from a database in rails?

Rails Delete operation using delete method Unlike the destroy method, with delete, you can remove a record directly from the database. Any dependencies to other records in the model are not taken into account. The method delete only deletes that one row in the database and nothing else.


I have a table without an ID column. When I try to delete from it using ActiveRecord the generated SQL is DELETE FROM table_name WHERE ID=NULL, which obviously doesn't work. Is there any way to delete from the table using ActiveRecord, or at least run a raw SQL delete query with placeholders (so it's not vulnerable to SQL injection)?