Friday, April 19, 2024

ActiveRecord::Base#pluck accepts hash values, devcontainers improvements and more!

Posted by vipulnsward

Hey everyone, Happy Friday! I hope you get some time to unwind and relax going into the weekend ๐Ÿ˜Ž

Vipul here with the latest updates for This Week in Rails. Letโ€™s dive in.

Allow ActiveRecord::Base#pluck to accept hash values
This change adds support for ActiveRecord::Base#pluck to accept hash values.

# Before
Post.joins(:comments).pluck("posts.id", "comments.id", "comments.body")

# After
Post.joins(:comments).pluck(posts: [:id], comments: [:id, :body])

The same applies to .pick, which is implemented using .pluck.

Fix child association loading in :n_plus_one_only mode
Strict loading in :n_plus_one_only mode is designed to prevent performance issues when deeply traversing associations. It allows Person.find(1).posts, but not Person.find(1).posts.map(&:category). This fix avoids the surprise that occurs when person.posts.first eagerly loads the whole association rather than allowing the user to manage the child association.

person = Person.find(1)
person.strict_loading!(mode: :n_plus_one_only)

# Before
person.posts.first
# SELECT * FROM posts WHERE person_id = 1; -- non-deterministic order

# After
person.posts.first # this is 1+1, not N+1
# SELECT * FROM posts WHERE person_id = 1 ORDER BY id LIMIT 1;

Add save_and_open_page helper to IntegrationTest
save_and_open_page is a capybara helper that lets developers inspect the status of the page at any given point in their tests. This change adds save_and_open_page helper support to allow using them from within system tests.

Change devcontainer.json to forward used ports for the project
Currently the generated devcontainer.json file does not forward the ports required for the project, so we might need to manually change it in order to access the project when running via devcontainer. This PR adds the required forwarding for the project by default.

Add node and yarn to devcontainer when creating a project with Javascript
Currently when creating a rails project with javascript (โ€“javascript=esbuild for example) neither node or yarn are added to the devcontainer. This change adds both when needed.

Fix typo in Feature Policy for idle-detection
This change fixes a small typo in feature policy idle-detection and not idle_detection which was leading to this policy not being applied before.

You can view the whole list of changes here.
We had 17 contributors to the Rails codebase this past week!

Until next time!

Subscribe to get these updates mailed to you.