RubyCademy Profile picture
The best Ruby code examples on 𝕏. To learn more lesser-known techniques and patterns: https://t.co/HeSrU9k9s3

Oct 20, 8 tweets

5 Essential Rails Shorthands You Should Know ⬇️
#ruby #rubyonrails

1️⃣ ActiveRecord::Relation#destroy_by 1️⃣

Use `destroy_by` instead of the classic `where(...).destroy`

2️⃣ Object#presence 2️⃣

Use the `presence` shorthand instead of checking if the object's value is non-blank using `Object#present?`

3️⃣ Object#in? 3️⃣

Under the hood, `in?` automatically chooses between `include?` and `cover?` based on the type of argument provided

4️⃣ ActiveRecord::FinderMethods#sole 4️⃣

Use `sole` to automatically handle cases where no record or multiple records are found, instead of manually raising errors!

5️⃣ Kernel#suppress 5️⃣

Use `suppress` instead of empty `rescue` blocks to elegantly ignore errors!

🧵 FIN 🧵

Thank you for taking the time to read this thread!  🙏

If you find it useful, feel free to like and repost each thread tweet!

Also, visit RubyCademy today for more PRO Tips and fun guides on Ruby and Rails!

rubycademy.com

Share this Scrolly Tale with your friends.

A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.

Keep scrolling