Skip to content

Commit

Permalink
A NullRelation should represent nothing. Closes rails#15176.
Browse files Browse the repository at this point in the history
[Matthew Draper & Yves Senn]
  • Loading branch information
senny committed Sep 11, 2014
1 parent 1245a9e commit 23f4e7b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
7 changes: 7 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
* A `NullRelation` should represent nothing. This fixes a bug where
`Comment.where(post_id: Post.none)` returned a non-empty result.

Closes #15176.

*Matthew Draper*, *Yves Senn*

* Include default column limits in schema.rb. Allows defaults to be changed
in the future without affecting old migrations that assumed old defaults.

Expand Down
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/relation/query_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -687,11 +687,11 @@ def lock!(locks = true) # :nodoc:
# end
#
def none
extending(NullRelation)
where("1=0").extending!(NullRelation)
end

def none! # :nodoc:
extending!(NullRelation)
where!("1=0").extending!(NullRelation)
end

# Sets readonly attributes for the returned relation. If value is
Expand Down
4 changes: 4 additions & 0 deletions activerecord/test/cases/relation/mutation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def relation_delegate_class(klass)
def attribute_alias?(name)
false
end

def sanitize_sql(sql)
sql
end
end

def relation
Expand Down
5 changes: 5 additions & 0 deletions activerecord/test/cases/relations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,11 @@ def test_null_relation_maximum
assert_equal nil, ac.engines.maximum(:id)
end

def test_null_relation_in_where_condition
assert_operator 0, :<, Comment.count # precondition, make sure there are comments.
assert_equal 0, Comment.where(post_id: Post.none).to_a.size
end

def test_joins_with_nil_argument
assert_nothing_raised { DependentFirm.joins(nil).first }
end
Expand Down

0 comments on commit 23f4e7b

Please sign in to comment.