mirror of
https://github.com/jbranchaud/til
synced 2026-01-07 09:08:01 +00:00
Add Mark For Destruction as a rails til
This commit is contained in:
@@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really
|
|||||||
warrant a full blog post. These are mostly things I learn by pairing with
|
warrant a full blog post. These are mostly things I learn by pairing with
|
||||||
smart people at [Hashrocket](http://hashrocket.com/).
|
smart people at [Hashrocket](http://hashrocket.com/).
|
||||||
|
|
||||||
_496 TILs and counting..._
|
_497 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -320,6 +320,7 @@ _496 TILs and counting..._
|
|||||||
- [Hash Slicing](rails/hash-slicing.md)
|
- [Hash Slicing](rails/hash-slicing.md)
|
||||||
- [Ignore Poltergeist JavaScript Errors](rails/ignore-poltergeist-javascript-errors.md)
|
- [Ignore Poltergeist JavaScript Errors](rails/ignore-poltergeist-javascript-errors.md)
|
||||||
- [List The Enqueued Jobs](rails/list-the-enqueued-jobs.md)
|
- [List The Enqueued Jobs](rails/list-the-enqueued-jobs.md)
|
||||||
|
- [Mark For Destruction](rails/mark-for-destruction.md)
|
||||||
- [Migrating Up Down Up](rails/migrating-up-down-up.md)
|
- [Migrating Up Down Up](rails/migrating-up-down-up.md)
|
||||||
- [Params Includes Submission Button Info](rails/params-includes-submission-button-info.md)
|
- [Params Includes Submission Button Info](rails/params-includes-submission-button-info.md)
|
||||||
- [Perform SQL Explain With ActiveRecord](rails/perform-sql-explain-with-activerecord.md)
|
- [Perform SQL Explain With ActiveRecord](rails/perform-sql-explain-with-activerecord.md)
|
||||||
|
|||||||
42
rails/mark_for_destruction.md
Normal file
42
rails/mark_for_destruction.md
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
# Mark For Destruction
|
||||||
|
|
||||||
|
Do you have some complicated logic or criteria for deleting associated
|
||||||
|
records? [ActiveRecord's
|
||||||
|
`#mark_for_destruction`](http://api.rubyonrails.org/classes/ActiveRecord/AutosaveAssociation.html#method-i-mark_for_destruction) may come in handy.
|
||||||
|
|
||||||
|
Let's say we have _users_ who author _articles_. We want to delete some of
|
||||||
|
the user's articles based on some criteria -- those articles that have odd
|
||||||
|
`id`s.
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
> user = User.first
|
||||||
|
#=> #<User...>
|
||||||
|
> user.articles.each { |a| a.mark_for_destruction if a.id.odd? }
|
||||||
|
#=> [#<Article...>, ...]
|
||||||
|
> user.articles.find(1).marked_for_destruction?
|
||||||
|
#=> true
|
||||||
|
> user.articles.find(2).marked_for_destruction?
|
||||||
|
#=> false
|
||||||
|
```
|
||||||
|
|
||||||
|
We've marked our articles for destruction and confirmed as much with the
|
||||||
|
[`#marked_for_destruction?`](http://api.rubyonrails.org/classes/ActiveRecord/AutosaveAssociation.html#method-i-marked_for_destruction-3F) method. Now, to go through with the destruction, we just have to save the parent record -- the user.
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
> user.save
|
||||||
|
(0.2ms) BEGIN
|
||||||
|
User Exists (0.8ms) SELECT 1 AS one FROM "users" WHERE ("users"."email" = 'person1@example.com' AND "users"."id" != 1) LIMIT 1
|
||||||
|
SQL (3.0ms) DELETE FROM "articles" WHERE "articles"."id" = $1 [["id", 1]]
|
||||||
|
SQL (0.2ms) DELETE FROM "articles" WHERE "articles"."id" = $1 [["id", 3]]
|
||||||
|
(2.1ms) COMMIT
|
||||||
|
=> true
|
||||||
|
```
|
||||||
|
|
||||||
|
Note: the parent record must have `autosave: true` declared on the
|
||||||
|
association.
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
class User < ActiveRecord::Base
|
||||||
|
has_many :articles, autosave: true
|
||||||
|
end
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user