Welcome to another edition of This Week in #dev, a series of posts where we bring some of our most interesting Slack conversations to the public.
Key Renaming with Hash#transform_keys
Joël Quenneville shares that Hash#transform_keys
allows you to pass a
map of old to new keys if you want to do a key rename:
{old: 1, dont_care: 2}.transform_keys(old: :new)
#=> {new: 1, dont_care: 2}
Sanitizing Input for a LIKE Query
Summer ☀️ mentions that Active Record has a sanitize_sql_like
method for
sanitizing input for a LIKE
query.
sanitize_sql_like("100% true!")
# => "100\\% true!"
sanitize_sql_like("snake_cased_string")
# => "snake\\_cased\\_string"
Toggling Attributes in Rails
Matheus Richard learned about Active Record’s toggle
method that assigns
the boolean opposite of an attribute.
user = User.first
user.banned? # => false
user.toggle(:banned)
user.banned? # => true
Thanks
This edition was brought to you by Joël Quenneville, Matheus Richard, and Summer ☀️. Thanks to all contributors! 🎉