This week in #dev (Apr 21, 2023)

Ask questions Research chat →

https://thoughtbot.com/blog/this-week-in-dev-apr-21-2023#a-handy-method-for-splitting-enumerables · scraped

ruby

Attachments

Scraped Content

— 439 words · 2026-02-14 17:42:11 UTC ·

Excerpt

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. Today we’re talking about: splitting arrays, handling deprecation warnings, and querying missing records. ## A Handy Method for Splitting Enumerables Steve Polito learned about Ruby’s Enumerable#partition. This method is useful for dividing an existing array into two separate arrays, he says. ```plain text # Before def call(appointments) appointments_today = [] appointments_next_week = [] appointments.each do |appointment| if appointment.starts_at.to_date == Date.current appointments_today << appointment else appointments_next_week << appointment end end end # After def call(appointments) appointments_today, appointments_next_week = appointments.partition do |appointment| appointment.starts_at.to_date == Date.current end end ``` ## Handling Ruby Deprecation Warnings Neil Carvalho discovers t
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. Today we’re talking about: splitting arrays, handling deprecation warnings, and querying missing records. ## A Handy Method for Splitting Enumerables Steve Polito learned about Ruby’s Enumerable#partition. This method is useful for dividing an existing array into two separate arrays, he says. ```plain text # Before def call(appointments) appointments_today = [] appointments_next_week = [] appointments.each do |appointment| if appointment.starts_at.to_date == Date.current appointments_today << appointment else appointments_next_week << appointment end end end # After def call(appointments) appointments_today, appointments_next_week = appointments.partition do |appointment| appointment.starts_at.to_date == Date.current end end ``` ## Handling Ruby Deprecation Warnings Neil Carvalho discovers that Ruby deprecation warnings have been disabled since Ruby 2.7.2 due to an excessive number of deprecation warnings related to keyword arguments. This concerns him, and he suggests setting Warning[:deprecated] to true in spec_helper.rb and asks for other suggestions. Summer ☀️ steps in and proposes logging warnings to an error-tracking service in production, while in development and testing, they recommend raising errors instead of relying on log messages that can be easily overlooked. It would be also important to be able to choose whether these actions apply to project code or dependencies, and selectively silence specific deprecation types or downgrade them from errors to warnings. Summer ☀️ mentions the usefulness of Shopify’s deprecation_toolkit, which offers these functionalities for both Warning and ActiveSupport::Deprecation. ## Querying Missing Associated Records Rémy Hannequin seeks assistance with an ActiveRecord query to retrieve records that are missing associated records with a specific attribute. He provides an example scenario of authors and books with different languages and wants to find authors who don’t have books written in Elvish: ```plain text # This is not what Remy wants Author .where.missing(:books) .where(books: {language: :elvish}) # That generates this SQL SELECT * FROM "authors" LEFT OUTER JOIN "books" ON "books"."author_id" = "authors"."id" WHERE "books"."id" IS NULL AND "books"."language" = "elvish" ``` Neil Carvalho responds with two possible approaches. If it makes sense to define a specific association for Elvish books in the Author model, Neil Carvalho suggests using Author.where.missing(:elvish_books) to query authors without associated Elvish books. ```plain text class Author has_many :elvish_books, -> { where(language: "elvish") }, class_name: "Book" end # Now this works! Author.where.missing(:elvish_books) ``` If it doesn’t make sense to define a separate association, the recommendation is using a left join: ```plain text Author.left_joins('books ON books.author_id = authors.id AND books.language = "elvish"') ``` ## Thanks This edition was brought to you by Neil Carvalho, Rémy Hannequin, Steve Polito, and Summer ☀️. Thanks to all contributors! 🎉 ![](https://prod-files-secure.s3.us-west-2.amazonaws.com/871f1661-80b8-4d0c-ac3b-2adfc6ff4c66/7ba74b8d-a52d-43b4-adec-964cbf969701/code-audit-223309dd776c6687fe7f835785a599c4872c3a04352495fb504764f9e21e8558.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=ASIAZI2LB466RBJTN7EC%2F20260214%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20260214T174211Z&X-Amz-Expires=3600&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEEEaCXVzLXdlc3QtMiJHMEUCIQD%2FpgI5WFHFJvR6nJ5v8lMQDfe915JTGJaWkhzmU%2F6qWAIga39scwyYTWYiTi%2FdXKyJ30Btu3DqLzNxXrh4FBMj9B0q%2FwMIChAAGgw2Mzc0MjMxODM4MDUiDJPIuQOdsisNi4XrTSrcA1H3PF1e5py9iRAxYjfkLd5mAe0pScWXk8WI6%2BtfmYASGIQ3vSKP3nMNotfuUopLL%2B9%2F2eHscWRu2cU8cBsA8ajK%2BHlndSIAWyqe6G%2Bchu8Y8Kt%2BigLiXbXmhtgo7jYT8w2CIwX83efRVxUMkz9fzehRusN8gfFEm8k1XchZAPkiixdi6u8%2FKX0iOBstHV6qtFceC2WsJxR%2B16sjSRJyMIUm%2F0teGaPo4gDvsq4JR5NNkFL8lP3BDk67UQqCpVlv2bnPhTAEA%2BlzAT95BHMufYjOS6H3MxsV0pvDL46BeCTr0qv6HMGX7g2KdSjzl%2FQBaqDSpE7V09AexQw8W4COTwpghWTOELr9BCwIjbUq03SMFrDatyaNSiJe%2FhHQ2M3e8ZJKE8LykTMi%2FsFy%2BOHgX40HgLL4Pl0wUdKOWkug5Je3qH0PGDHVNr%2B1ac6OcOHOVyj1L%2BOZ4xBLLofujWl8LJ8c7ed%2FSYp1gJYYgharUfeI3AWTBOF90L00IqeW03FceqefNESgaYwL1hKZ1OgWCMrbVESoiLQqSA0uWFxtyLV4qjx5S2nGfPogyKme%2BwEfNIB%2FyEimnCew49VXktRtc8uUlUl130kg1piVQnghwdxbTECD1iiWr9cpQZmNMObRwswGOqUBDL0d7HbqTE6ofn%2BxAKhthsTh7Z3WzyufWahj3R8TdqCz8JYvVWuHf0hnOfsw0le3DundL91nLMajflNBhwZ26mbkFaz%2Fa3K5C%2BGVbD%2BYDb5p6GZYP%2FIBELsV9P5ykX%2BFKTosijQzMvi3JYJ6YZq1ggUS%2B%2FuXJDfgtIcgFH4jRPrKX1sOwl0D1MrNXuszsiiGa0%2Fxq8GZUSFTkTZhVF%2BKh%2BEI7G0L&X-Amz-Signature=feafcb82b9cdce4300700d100382652ffe513af15a19576fb87709d15b103f6f&X-Amz-SignedHeaders=host&x-amz-checksum-mode=ENABLED&x-id=GetObject)

Visibility

Visible to everyone

Reading Status

Related Bookmarks

My Note


Saved!

Annotations

Export as Markdown
+ Annotate selection

Add Annotation