Skip to content

Commit 4416874

Browse files
soartec-labEdouard-chin
authored andcommitted
Add direct insert and direct delete methods to a guide [skip ci]
1 parent fa6886b commit 4416874

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

guides/source/active_record_basics.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -425,9 +425,7 @@ book.id # => 107
425425
# Now the `book` record is committed to the database and has an `id`.
426426
```
427427

428-
Finally, if a block is provided, both `create` and `new` will yield the new
429-
object to that block for initialization, while only `create` will persist the
430-
resulting object to the database:
428+
If a block is provided, both `create` and `new` will yield the new object to that block for initialization, while only `create` will persist the resulting object to the database:
431429

432430
```ruby
433431
book = Book.new do |b|
@@ -447,6 +445,14 @@ something like this:
447445
INSERT INTO "books" ("title", "author", "created_at", "updated_at") VALUES (?, ?, ?, ?) RETURNING "id" [["title", "Metaprogramming Ruby 2"], ["author", "Paolo Perrotta"], ["created_at", "2024-02-22 20:01:18.469952"], ["updated_at", "2024-02-22 20:01:18.469952"]]
448446
```
449447

448+
Finally, if you'd like to insert several records **without callbacks or
449+
validations**, you can directly insert records into the database using `insert` or `insert_all` methods:
450+
451+
```ruby
452+
Book.insert(title: "The Lord of the Rings", author: "J.R.R. Tolkien")
453+
Book.insert_all([{ title: "The Lord of the Rings", author: "J.R.R. Tolkien" }])
454+
```
455+
450456
### Read
451457

452458
Active Record provides a rich API for accessing data within a database. You can
@@ -578,6 +584,14 @@ Book.destroy_by(author: "Douglas Adams")
578584
Book.destroy_all
579585
```
580586

587+
Additionally, if you'd like to delete several records **without callbacks or
588+
validations**, you can delete records directly from the database using `delete` and `delete_all` methods:
589+
590+
```ruby
591+
Book.find_by(title: "The Lord of the Rings").delete
592+
Book.delete_all
593+
```
594+
581595
Validations
582596
-----------
583597

0 commit comments

Comments
 (0)