mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
automatically add references when we can
This commit is contained in:
parent
4429f16c0a
commit
ee7f666035
3 changed files with 29 additions and 1 deletions
|
@ -21,6 +21,18 @@ module ActiveRecord
|
|||
predicates.flatten
|
||||
end
|
||||
|
||||
def self.references(attributes)
|
||||
references = attributes.map do |key, value|
|
||||
if value.is_a?(Hash)
|
||||
key
|
||||
else
|
||||
key = key.to_s
|
||||
key.split('.').first.to_sym if key.include?('.')
|
||||
end
|
||||
end
|
||||
references.compact
|
||||
end
|
||||
|
||||
private
|
||||
def self.build(attribute, value)
|
||||
case value
|
||||
|
|
|
@ -52,7 +52,7 @@ module ActiveRecord
|
|||
return self if args.blank?
|
||||
|
||||
relation = clone
|
||||
relation.references_values = (references_values + args).uniq
|
||||
relation.references_values = (references_values + args).flatten.uniq
|
||||
relation
|
||||
end
|
||||
|
||||
|
@ -151,6 +151,7 @@ module ActiveRecord
|
|||
return self if opts.blank?
|
||||
|
||||
relation = clone
|
||||
relation = relation.references(PredicateBuilder.references(opts)) if Hash === opts
|
||||
relation.where_values += build_where(opts, rest)
|
||||
relation
|
||||
end
|
||||
|
@ -159,6 +160,7 @@ module ActiveRecord
|
|||
return self if opts.blank?
|
||||
|
||||
relation = clone
|
||||
relation = relation.references(PredicateBuilder.references(opts)) if Hash === opts
|
||||
relation.having_values += build_where(opts, rest)
|
||||
relation
|
||||
end
|
||||
|
|
|
@ -1175,4 +1175,18 @@ class RelationTest < ActiveRecord::TestCase
|
|||
scope = Post.references(:comments)
|
||||
assert !scope.eager_loading?
|
||||
end
|
||||
|
||||
def test_automatically_added_references
|
||||
scope = Post.where(:comments => { :body => "Bla" })
|
||||
assert_equal [:comments], scope.references_values
|
||||
|
||||
scope = Post.where('comments.body' => 'Bla')
|
||||
assert_equal [:comments], scope.references_values
|
||||
|
||||
scope = Post.having(:comments => { :body => "Bla" })
|
||||
assert_equal [:comments], scope.references_values
|
||||
|
||||
scope = Post.having('comments.body' => 'Bla')
|
||||
assert_equal [:comments], scope.references_values
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue