diff options
author | Akira Matsuda <ronnie@dio.jp> | 2012-11-05 13:56:30 +0900 |
---|---|---|
committer | Akira Matsuda <ronnie@dio.jp> | 2012-11-30 01:18:18 +0900 |
commit | de75af7acc5c05c708443de40e78965925165217 (patch) | |
tree | 2eb07baa89d145f0d0d66ee51d5fb0e5d62646e6 /activerecord/test/cases/relation | |
parent | 80b8df5f3d09205d8f239e9aefd5ed0e0ddfd4a5 (diff) | |
download | rails-de75af7acc5c05c708443de40e78965925165217.tar.gz rails-de75af7acc5c05c708443de40e78965925165217.tar.bz2 rails-de75af7acc5c05c708443de40e78965925165217.zip |
Relation.where with no args can be chained with not, like, and not_like
examples:
Model.where.not field: nil
#=> "SELECT * FROM models WHERE field IS NOT NULL
Model.where.like name: 'Jeremy%'
#=> "SELECT * FROM models WHERE name LIKE 'Jeremy%'
this feature was originally suggested by Jeremy Kemper https://github.com/rails/rails/pull/5950#issuecomment-5591330
Closes #5950
Diffstat (limited to 'activerecord/test/cases/relation')
-rw-r--r-- | activerecord/test/cases/relation/where_chain_test.rb | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/activerecord/test/cases/relation/where_chain_test.rb b/activerecord/test/cases/relation/where_chain_test.rb new file mode 100644 index 0000000000..10b19da3fd --- /dev/null +++ b/activerecord/test/cases/relation/where_chain_test.rb @@ -0,0 +1,78 @@ +require 'cases/helper' +require 'models/post' +require 'models/comment' + +module ActiveRecord + class WhereChainTest < ActiveRecord::TestCase + fixtures :posts + + def test_not_eq + expected = Arel::Nodes::NotEqual.new(Post.arel_table[:title], 'hello') + relation = Post.where.not(title: 'hello') + assert_equal([expected], relation.where_values) + end + + def test_not_null + expected = Arel::Nodes::NotEqual.new(Post.arel_table[:title], nil) + relation = Post.where.not(title: nil) + assert_equal([expected], relation.where_values) + end + + def test_not_in + expected = Arel::Nodes::NotEqual.new(Post.arel_table[:title], %w[hello goodbye]) + relation = Post.where.not(title: %w[hello goodbye]) + assert_equal([expected], relation.where_values) + end + + def test_association_not_eq + expected = Arel::Nodes::NotEqual.new(Comment.arel_table[:title], 'hello') + relation = Post.joins(:comments).where.not(comments: {title: 'hello'}) + assert_equal(expected.to_sql, relation.where_values.first.to_sql) + end + + def test_not_eq_with_preceding_where + relation = Post.where(title: 'hello').where.not(title: 'world') + + expected = Arel::Nodes::Equality.new(Post.arel_table[:title], 'hello') + assert_equal(expected, relation.where_values.first) + + expected = Arel::Nodes::NotEqual.new(Post.arel_table[:title], 'world') + assert_equal(expected, relation.where_values.last) + end + + def test_not_eq_with_succeeding_where + relation = Post.where.not(title: 'hello').where(title: 'world') + + expected = Arel::Nodes::NotEqual.new(Post.arel_table[:title], 'hello') + assert_equal(expected, relation.where_values.first) + + expected = Arel::Nodes::Equality.new(Post.arel_table[:title], 'world') + assert_equal(expected, relation.where_values.last) + end + + def test_like + expected = Arel::Nodes::Matches.new(Post.arel_table[:title], 'a%') + relation = Post.where.like(title: 'a%') + assert_equal([expected], relation.where_values) + end + + def test_not_like + expected = Arel::Nodes::DoesNotMatch.new(Post.arel_table[:title], 'a%') + relation = Post.where.not_like(title: 'a%') + assert_equal([expected], relation.where_values) + end + + def test_chaining_multiple + relation = Post.where.like(title: 'ruby on %').where.not(title: 'ruby on rails').where.not_like(title: '% ales') + + expected = Arel::Nodes::Matches.new(Post.arel_table[:title], 'ruby on %') + assert_equal(expected, relation.where_values[0]) + + expected = Arel::Nodes::NotEqual.new(Post.arel_table[:title], 'ruby on rails') + assert_equal(expected, relation.where_values[1]) + + expected = Arel::Nodes::DoesNotMatch.new(Post.arel_table[:title], '% ales') + assert_equal(expected, relation.where_values[2]) + end + end +end |