This week in #dev (Apr 28, 2023)

thoughtbot
Edited by Matheus Richard

Welcome to another edition of This Week in #dev, a series of posts where we bring some of the most interesting Slack conversations to the public. This is the Today I Learned edition.

Active Record’s rewhere

Matheus Sales learned about the rewhere method in Active Record. It’s a query method that allows you to change a previously set where condition for a given attribute instead of appending to that condition.

Post.where(published: true).rewhere(published: false) # => WHERE `published` = 0

It can be handy for overriding a default_scope or when building an index page with a couple of filters, and one of those must override previous ones applied.

There’s also reorder and reselect, Neil Carvalho adds.

Stashing Untracked Files

Steve Polito shares that he learns that it’s possible to stash untracked files with git stash --untracked.

$ git status
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        README.md
​
$ git stash
No local changes to save
​
$ git stash --include-untracked
Saved working directory and index state WIP on...

Ruby’s Date.parse Format Preference

Neil Carvalho discovered that Ruby’s Date.parse prefers the DD/MM/YYYY format over MM/DD/YYYY.

date = Date.parse "02/01/2023"
# => #<Date: 2023-01-02 ((2459947j,0s,0n),+0s,2299161j)>

date.day
# => 2
date.month
# => 1

Today I Learned

In a TIL-meta fashion, Sarah Lima shares that she learned about thoughtbot’s TIL repo.

Thanks

This edition was brought to you by: Matheus Sales, Neil Carvalho, Sarah Lima, and Steve Polito. Thanks to all contributors! 🎉