rails icon indicating copy to clipboard operation
rails copied to clipboard

Pattern matching APIs for records and relations.

Open kddnewton opened this issue 1 year ago • 6 comments

This provides the Ruby 2.7+ pattern matching interface for Active Record records and relations. It allows the user to pattern match against attributes and associations on records through a hash pattern. It also allows the user to pattern match against relations through an array pattern.

class Post < ActiveRecord::Base
  has_many :comments
end

class Comment < ActiveRecord::Base
  belongs_to :post
end

post = Post.new(title: "Welcome!", comments: [Comment.new(body: "Thanks!")])
post => { title: "Welcome!", comments: [Comment[body:]] }
body # => "Thanks!"

kddnewton avatar May 11 '22 17:05 kddnewton

cc @gmcgibbon since you kindly merged the last one

kddnewton avatar May 11 '22 17:05 kddnewton

What's the status of this? Still blocked? I've been using this in production and it's working great.

ezekg avatar Aug 10 '22 14:08 ezekg

Here you can read the status of this https://github.com/rails/rails/pull/45553#issuecomment-1179568855

rafaelfranca avatar Aug 10 '22 14:08 rafaelfranca

@rafaelfranca, I saw that but @kddnewton hasn't gotten a response in nearly a month. I agree with his reasoning.

Would hate to see this get lost. I don't see a problem with the current API.

ezekg avatar Aug 10 '22 14:08 ezekg

We don't need to rush a decision here. Rails 7.1 isn't due any time soon and this feature would only be release in Rails 7.1. Rushing a making the wrong decision will just cause trouble for us maintainers, so it is better to make sure we have considered all possibilities.

rafaelfranca avatar Aug 10 '22 15:08 rafaelfranca

Understood, @rafaelfranca. Is there a location where this discussion is taking place?

ezekg avatar Aug 10 '22 17:08 ezekg

In the meantime, I've released a gem https://github.com/kddnewton/rails-pattern_matching that encapsulates this pattern. Folks can maybe use that to prototype/test what they think is the right approach. Hopefully that will help aid in a decision.

kddnewton avatar Dec 07 '22 03:12 kddnewton

For my own work-tracking purposes I'm going to close this PR, but I will continue to maintain the gem. If there comes a time where rails would like to consider pattern matching, give me a shout!

kddnewton avatar Mar 04 '24 18:03 kddnewton