Add ActiveRecord::Relation#extract_associated for extracting associated record - #35784
Conversation
…ated records from a relation
|
|
||
| def test_extracted_association | ||
| authors = Post.all.extract_associated(:author) | ||
| assert_equal Post.all.collect(&:author), authors |
There was a problem hiding this comment.
Perhaps we should wrap the extract in an assert_queries 1, such that we test the preload query optimization.
|
🤔 what about |
|
Load associated is the same as preload. The important part here is that the records returned are those of the association, not of the relation. Extract is the word for taking something like that.
… On Mar 28, 2019, at 16:41, Josef Šimánek ***@***.***> wrote:
🤔 what about load_associated?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
|
I'm just trying to find out something similar in current method names since I understand this as a |
|
As it is, I feel I actually think the current behavior can be quite confusing. As the doc states, account.memberships.extract_associated(:user)
# => Returns collection of User recordsYou could easily expect the following to return a “collection of Comment records”: category.posts.extract_associated(:comments)…but you’d be wrong, as this will instead return a “collection of Comment collections”. |
Add `ActiveRecord::Relation#extract_associated` for extracting associated record rails/rails#35784
It's sometimes more convenient to access a certain set of records by going through a scoped relation and its associations. It's easy enough to do this today using #preload and #collect, but it's repetitive and not descriptive of the intent.
This is where
#extract_associatedcomes in. Like so:Which is short-hand for and describing the intent of: