Skip to content
This repository
Browse code

Merge pull request #8452 from claudiob/explain_where_chain_parameters

Document the types of arguments accepted by AR#not
  • Loading branch information...
commit c2052846f4551d9eb24bb66ce7b05b23abc6cd1f 2 parents 27ca915 + bb53c60
Carlos Antonio da Silva carlosantoniodasilva authored
13 activerecord/lib/active_record/relation/query_methods.rb
@@ -14,6 +14,15 @@ def initialize(scope)
14 14 # Returns a new relation expressing WHERE + NOT condition
15 15 # according to the conditions in the arguments.
16 16 #
  17 + # #not accepts conditions in one of these formats: String, Array, Hash.
  18 + # See #where for more details on each format.
  19 + #
  20 + # User.where.not("name = 'Jon'")
  21 + # # SELECT * FROM users WHERE name <> 'Jon'
  22 + #
  23 + # User.where.not(["name = ?", "Jon"])
  24 + # # SELECT * FROM users WHERE name <> 'Jon'
  25 + #
17 26 # User.where.not(name: "Jon")
18 27 # # SELECT * FROM users WHERE name <> 'Jon'
19 28 #
@@ -40,7 +49,7 @@ def not(opts, *rest)
40 49 end
41 50
42 51 # Returns a new relation expressing WHERE + LIKE condition
43   - # according to the conditions in the arguments.
  52 + # according to the conditions provided as a hash in the arguments.
44 53 #
45 54 # Book.where.like(title: "Rails%")
46 55 # # SELECT * FROM books WHERE title LIKE 'Rails%'
@@ -53,7 +62,7 @@ def like(opts, *rest)
53 62 end
54 63
55 64 # Returns a new relation expressing WHERE + NOT LIKE condition
56   - # according to the conditions in the arguments.
  65 + # according to the conditions provided as a hash in the arguments.
57 66 #
58 67 # Conference.where.not_like(name: "%Kaigi")
59 68 # # SELECT * FROM conferences WHERE name NOT LIKE '%Kaigi'
12 activerecord/test/cases/relation/where_chain_test.rb
@@ -50,6 +50,18 @@ def test_not_eq_with_succeeding_where
50 50 assert_equal(expected, relation.where_values.last)
51 51 end
52 52
  53 + def test_not_eq_with_string_parameter
  54 + expected = Arel::Nodes::Not.new("title = 'hello'")
  55 + relation = Post.where.not("title = 'hello'")
  56 + assert_equal([expected], relation.where_values)
  57 + end
  58 +
  59 + def test_not_eq_with_array_parameter
  60 + expected = Arel::Nodes::Not.new("title = 'hello'")
  61 + relation = Post.where.not(['title = ?', 'hello'])
  62 + assert_equal([expected], relation.where_values)
  63 + end
  64 +
53 65 def test_like
54 66 expected = Arel::Nodes::Matches.new(Post.arel_table[:title], 'a%')
55 67 relation = Post.where.like(title: 'a%')

0 comments on commit c205284

Please sign in to comment.
Something went wrong with that request. Please try again.