> orders.map(&:order_option).map(&:item).map(&:title)
=> ['Foo', 'Bar', 'Baz']
> orders.map { |order| order&.order_option&.item&.title }
> orders.map(&:order_option, &:item, &:title)
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-co...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
On May 11, 2018, at 1:10 PM, Alberto Almagro <alberto...@gmail.com> wrote:These days I have been comparing records in my daily job lots of times, which made me think about a better way to retrieve and compare them. When I want to navigate through several relations in a collection I often see myself writing code like the following:Given orders as a collection of Order:> orders.map(&:order_option).map(&:item).map(&:title)=> ['Foo', 'Bar', 'Baz']
That is, chaining maps with Symbol to Proc coercions one after each other. Sharing my thoughts with my company's architect we came up with the alternative:> orders.map { |order| order&.order_option&.item&.title }
But we agreed that the notation was awful and didn't improve what we had before. With this, I proposed what I think it is more like what we would expect Ruby to have. I would like to add a notation similar to the one we can find at Array#dig or Hash#dig in the following manner:> orders.map(&:order_option, &:item, &:title)The method doesn't necessarily need to be named map or collect, we can agree on a different name for it if you want. Please share your thoughts with me. If you like this, I would be very happy to write a PR to include it in Rails.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-core@googlegroups.com.
--
You received this message because you are subscribed to a topic in the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rubyonrails-core/22K3pcKEZZU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rubyonrails-core+unsubscribe@googlegroups.com.
--
You received this message because you are subscribed to a topic in the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rubyonrails-core/22K3pcKEZZU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rubyonrails-core+unsubscribe@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-co...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to a topic in the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rubyonrails-core/22K3pcKEZZU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rubyonrails-co...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.