aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation/query_methods.rb
diff options
context:
space:
mode:
authorYves Senn <yves.senn@gmail.com>2014-01-30 10:46:06 +0100
committerYves Senn <yves.senn@gmail.com>2014-01-30 10:46:06 +0100
commit9632c986b4df02cc6c51dbbc2768403bd8e8c07f (patch)
tree642ec94703da0e5c544fa1803c0278f02509be75 /activerecord/lib/active_record/relation/query_methods.rb
parent1917293dae366d5d6028da351460c8bccf22d21f (diff)
downloadrails-9632c986b4df02cc6c51dbbc2768403bd8e8c07f.tar.gz
rails-9632c986b4df02cc6c51dbbc2768403bd8e8c07f.tar.bz2
rails-9632c986b4df02cc6c51dbbc2768403bd8e8c07f.zip
docs, `references` is only used with `includes`. Closes #13727.
There is no gain in `referencing` tables that are not used for preloading. Furthermore it will break if polymorphic associations are invloved. This is because `references_eager_loaded_tables?` uses all `reference_values` to decide wether to `eager_load` or `preload`.
Diffstat (limited to 'activerecord/lib/active_record/relation/query_methods.rb')
-rw-r--r--activerecord/lib/active_record/relation/query_methods.rb23
1 files changed, 14 insertions, 9 deletions
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb
index 88fc47fada..14470f22aa 100644
--- a/activerecord/lib/active_record/relation/query_methods.rb
+++ b/activerecord/lib/active_record/relation/query_methods.rb
@@ -120,6 +120,9 @@ module ActiveRecord
# Will throw an error, but this will work:
#
# User.includes(:posts).where('posts.name = ?', 'example').references(:posts)
+ #
+ # Note that +includes+ works with association names while +references+ needs
+ # the actual table name.
def includes(*args)
check_if_method_has_arguments!(:includes, args)
spawn.includes!(*args)
@@ -163,24 +166,26 @@ module ActiveRecord
self
end
- # Used to indicate that an association is referenced by an SQL string, and should
- # therefore be JOINed in any query rather than loaded separately.
+ # 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 conjuction with +includes+.
+ # See #includes for more details.
#
# User.includes(:posts).where("posts.name = 'foo'")
# # => Doesn't JOIN the posts table, resulting in an error.
#
# User.includes(:posts).where("posts.name = 'foo'").references(:posts)
# # => Query now knows the string references posts, so adds a JOIN
- def references(*args)
- check_if_method_has_arguments!(:references, args)
- spawn.references!(*args)
+ def references(*table_names)
+ check_if_method_has_arguments!(:references, table_names)
+ spawn.references!(*table_names)
end
- def references!(*args) # :nodoc:
- args.flatten!
- args.map!(&:to_s)
+ def references!(*table_names) # :nodoc:
+ table_names.flatten!
+ table_names.map!(&:to_s)
- self.references_values |= args
+ self.references_values |= table_names
self
end