This Week in Rails

March 17, 2023

TestFixtures#fixture_path deprecation, FinderMethods#find support for composite primary key values, etc.

Kaixo! Long time no see. Emmanuel Hayford here. This week, a lot happened in Rails, so much so that to keep this edition short, I've had to cut some equally good pull requests out. Here's what I have for you today:

Allow querying by the whole record for composite query constraints
Suppose you have defined an association between two models, BlogPost and Comment, with composite query constraints using the has_many method:

class BlogPost
  has_many :comments, query_constraints: [:blog_id, :blog_post_id]

You can now query blog posts that are associated with a specific set of comments by passing the comments objects as a parameter to the where method:

comments = Comment.first(2)
BlogPost.where(comments: comments).to_a

For the time being, however, association types like belongs_to, has_one, has_many_through and polymorphic associations are not covered. You can take a dig at this if you fancy!

ActiveRecord::FinderMethods#find support for composite pk values
ActiveRecord::FinderMethods#find now supports passing sets of composite primary key values like:

Cpk::Book.find([[1, 1], [1, 2]])

and treats values as values of the composite primary key columns but only for models with the primary_key being an Array.

Use Thor for built-in restart task
This is part of an effort to migrate bin/rails from Rake to Thor, but a nice side-effect of this change is that you can now call bin/rails restart outside of the app directory, like so:

my_app/bin/rails restart

Adds documentation for Arel::Nodes::Node
Arel is a private API, meaning there haven't been any meaningful efforts to document it in the past. This hasn't stopped gem authors and application programmers from using it -- we might as well document it, and that is what this pull request does with a significant portion of some documentation for Arel for anyone who needs it.

Introduce TestFixtures#fixture_paths
In this one, we deprecate TestFixtures#fixture_path in favour of TestFixtures#fixture_paths.

With this update, it is now possible to specify multiple fixture paths in your Rails application by using the fixture_paths accessor. By default, the test/fixtures directory will continue to serve as the primary fixture path for your app. However, if you need to include fixtures from other directories, you can specify additional fixture paths using the fixture_paths method.

This allows for something like:

ActiveSupport::TestCase.fixture_paths << "component1/test/fixtures"
ActiveSupport::TestCase.fixture_paths << "component2/test/fixtures"

Delegated Type supports customisable foreign_type column
The {role}_class method now respects the foreign_type option when used with the delegated_type method. This means that you can use delegated_type with non-conventional {role}_type column names by specifying the foreign_type option. This option is forwarded to the underlying belongs_to association that delegated_type wraps, allowing you to specify the name of the foreign key type column.

I'd like to share some conference news: RailsConf Atlanta is coming up on April 24th - April 26th 2023, you can still buy your tickets now if you plan on attending. RubyKaigi is also not far off on May 11th - 13th, 2023.

We saw 32 contributors this past week! See you next week.

About This Week in Rails

Your weekly inside scoop of interesting commits, pull requests and more from Rails.