---
title: 'This week in #dev (Dec 30, 2022)'
teaser: 'Highlights of what happened in our #dev channel on Slack this week.

  '
tags: this week in dev,rails,ruby,html,debugging
author: thoughtbot
published_on: 2023-01-10
---

Hey, happy new year! Welcome to the first 2023 edition of This Week in #dev, a
series of posts where we bring some of the most interesting Slack conversations
to the public. Today we're talking about hiding HTML content, Active Record
methods, and debugging system tests.

## HTML's `hidden` Global Attribute

[Steve Polito] has discovered a global attribute in HTML that can be used to hide
content from the user. This attribute is hidden from all presentations,
including screen readers. You can read more about it [here].

```html
<p>This content should be read right now, as it is important. I am so glad you are able to find it!</p>
<p hidden>This content is not relevant to this page right now, so should not be seen. Nothing to see here. Nada.</p>
```

[here]: https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/hidden

## Using `destroy_async` on `has_many` Associations

[Rémy Hannequin] discussed the use of the `dependent: :destroy_async` option
for `has_many` associations in Ruby on Rails. This option will enqueue an
`ActiveRecord::DestroyAssociationAsyncJob` job which will call destroy on the
associated objects, but Active Job must be set up for it to work.

<aside class="warn">
  As shared by <a href="https://thoughtbot.com/authors/neil-carvalho">Neil Carvalho</a>,
  <a href="https://edgeguides.rubyonrails.org/association_basics.html#options-for-belongs-to-dependent">you cannot use</a>
  this option if the association is backed by foreign key constraints in your database.
</aside>

## The Benefits of `Enumerator#with_index`

[Neil Carvalho] explained that `Enumerator#with_index` can take an offset argument,
which is useful when you need to start the index at a number other than 0.

```rb
# Note the `each` to build an enumerator
["first", "second", "third"].each.with_index(1) do |element, index|
  # element => "first" / index => 1
  # element => "second" / index => 2
  # element => "third" / index => 3
end
```

## Active Record Query Methods: `in_order_of`

[Matheus Richard] suggested using the [Active Record method `in_order_of`] method
to order records in a specific order.

```rb
User.in_order_of(:id, [1, 5, 3])
# This generates this SQL query:
# SELECT "users".* FROM "users"
#   WHERE "users"."id" IN (1, 5, 3)
#   ORDER BY CASE
#     WHEN "users"."id" = 1 THEN 1
#     WHEN "users"."id" = 5 THEN 2
#     WHEN "users"."id" = 3 THEN 3
#   END ASC
#
# => [#<User id: 1, name: "First User">,
#     #<User id: 5, name: "Fifth User">,
#     #<User id: 3, name: "Third User">]
```

[active record method `in_order_of`]: https://edgeapi.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-in_order_of

## Debugging with Debugger vs. Binding.pry

[Neil][neil carvalho] also discovered a difference between using `debugger` and
`binding.pry` to pause a system test using Capybara. `debugger` pauses the
entire program execution, while `binding.pry` pauses only the spec. With
`binding.pry`, the background process running the system spec server is not
paused, allowing the user to manually click and inspect the network effects of
that click.

## Thanks

This edition was brought to you by [Matheus Richard], [Neil Carvalho], [Steve
Polito], and [Rémy Hannequin]. Thanks to all contributors! 🎉

[matheus richard]: https://thoughtbot.com/blog/authors/matheus-richard
[neil carvalho]: https://thoughtbot.com/blog/authors/neil-carvalho
[steve polito]: https://thoughtbot.com/blog/authors/steve-polito
[rémy hannequin]: https://thoughtbot.com/blog/authors/remy-hannequin
