aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation/query_methods.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2019-03-29 16:01:45 -0700
committerGitHub <noreply@github.com>2019-03-29 16:01:45 -0700
commit4e076b03b6312b928a845f9a31288d10d7243338 (patch)
tree0c3a4a9316ee19626dcdaabec7c33e355ba03c56 /activerecord/lib/active_record/relation/query_methods.rb
parent406d3a926cfcd3724f8002f70346aad95eed4a8c (diff)
downloadrails-4e076b03b6312b928a845f9a31288d10d7243338.tar.gz
rails-4e076b03b6312b928a845f9a31288d10d7243338.tar.bz2
rails-4e076b03b6312b928a845f9a31288d10d7243338.zip
Add `ActiveRecord::Relation#extract_associated` for extracting associated record (#35784)
* Add `ActiveRecord::Relation#extract_associated` for extracting associated records from a relation
Diffstat (limited to 'activerecord/lib/active_record/relation/query_methods.rb')
-rw-r--r--activerecord/lib/active_record/relation/query_methods.rb13
1 files changed, 13 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb
index 6f0f2125dc..c37855172b 100644
--- a/activerecord/lib/active_record/relation/query_methods.rb
+++ b/activerecord/lib/active_record/relation/query_methods.rb
@@ -154,6 +154,19 @@ module ActiveRecord
self
end
+ # Extracts a named +association+ from the relation. The named association is first preloaded,
+ # then the individual association records are collected from the relation. Like so:
+ #
+ # account.memberships.extract_associated(:user)
+ # # => Returns collection of User records
+ #
+ # This is short-hand for:
+ #
+ # account.memberships.preload(:user).collect(&:user)
+ def extract_associated(association)
+ preload(association).collect(&association)
+ end
+
# Use to indicate that the given +table_names+ are referenced by an SQL string,
# and should therefore be JOINed in any query rather than loaded separately.
# This method only works in conjunction with #includes.