Skip to content

Commit

Permalink
Add support for Set to Relation#where
Browse files Browse the repository at this point in the history
Previously `#where` used to treat `Set`objects as nil, but now it treats
them as an array:

  set = Set.new([1, 2])
  Author.where(:id => set)
  # => SELECT "authors".* FROM "authors" WHERE "authors"."id" IN (1, 2)
  • Loading branch information
yuki24 committed Apr 14, 2015
1 parent 8ac458a commit 50cae60
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,3 +1,7 @@
* Add support for Set to `#where`.

*Yuki Nishijima*

* Fixed a bug where uniqueness validations would error on out of range values,
even if an validation should have prevented it from hitting the database.

Expand Down
Expand Up @@ -20,6 +20,7 @@ def initialize(table)
register_handler(Range, RangeHandler.new(self))
register_handler(Relation, RelationHandler.new)
register_handler(Array, ArrayHandler.new(self))
register_handler(Set, ArrayHandler.new(self))
register_handler(AssociationQueryValue, AssociationQueryHandler.new(self))
end

Expand Down
11 changes: 11 additions & 0 deletions activerecord/test/cases/relations_test.rb
Expand Up @@ -848,6 +848,17 @@ def test_find_all_using_where_with_relation_with_select_to_build_subquery
}
end

def test_find_all_using_where_with_a_set
david = authors(:david)
mary = authors(:mary)
set = Set.new([david.id, mary.id])

assert_queries(1) {
relation = Author.where(:id => set)
assert_equal [david, mary], relation
}
end

def test_exists
davids = Author.where(:name => 'David')
assert davids.exists?
Expand Down

0 comments on commit 50cae60

Please sign in to comment.