From 473812f4cec1b2badaf83b9f97aab3ca834a19fa Mon Sep 17 00:00:00 2001 From: Dan McClain Date: Fri, 30 Nov 2012 09:02:21 -0500 Subject: [PATCH] Fixes array_overlap calls and updates documentation --- README.md | 20 ++++++++++++++++---- spec/arel/array_spec.rb | 13 +++++++++---- spec/spec_helper.rb | 1 + 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 173dade..1975ce5 100644 --- a/README.md +++ b/README.md @@ -187,9 +187,13 @@ predicate for the `&&` operator. ```ruby user_arel = User.arel_table - -User.where(user_arel[:tags].array_overlap(['one','two'])).to_sql +query = user_arel.where(user_arel[:tags].array_overlap(['one','two'])) + .project(Arel.sql('*')) +query.to_sql # => SELECT \"users\".* FROM \"users\" WHERE \"users\".\"tags\" && '{one,two}' + +# Execute the query +User.find_by_sql(query.to_sql) ``` #### ANY or ALL functions @@ -219,10 +223,13 @@ user_arel = User.arel_table any_tags_function = Arel::Nodes::NamedFunction.new('ANY', [user_arel[:tags]]) predicate = Arel::Nodes::Equality.new('test', any_tags_function) +query = user_arel.where(predicate).project(Arel.sql('*')) -User.where(predicate).to_sql +query.to_sql #=> SELECT \"users\".* FROM \"users\" WHERE 'test' = ANY(\"users\".\"tags\") +# Execute the query +User.find_by_sql(query.to_sql) ``` The ALL version of this same predicate can be generated by swap `'ANY'` @@ -249,9 +256,14 @@ predicate for the `<<` operator. ```ruby user_arel = User.arel_table +query = user_arel.where(user_arel[:ip_address].contained_witin('127.0.0.1/24')) + .project(Arel.sql('*')) -User.where(user_arel[:ip_address].contained_witin('127.0.0.1/24')).to_sql +query.to_sql # => SELECT \"users\".* FROM \"users\" WHERE \"users\".\"ip_address\" << '127.0.0.1/24' + +# Execute the query +User.find_by_sql(query.to_sql) ``` ## Indexes diff --git a/spec/arel/array_spec.rb b/spec/arel/array_spec.rb index fcec216..e5d1e50 100644 --- a/spec/arel/array_spec.rb +++ b/spec/arel/array_spec.rb @@ -37,10 +37,15 @@ class ArelArray < ActiveRecord::Base two = ArelArray.create!(:tags => ['two']) arel_table = ArelArray.arel_table - ArelArray.where(arel_table[:tags].array_overlap(['one'])).should include(one) - ArelArray.where(arel_table[:tags].array_overlap(['two'])).should include(two) - ArelArray.where(arel_table[:tags].array_overlap(['two','one'])).should include(two) - ArelArray.where(arel_table[:tags].array_overlap(['two','one'])).should include(one) + query = arel_table.where(arel_table[:tags].array_overlap(['one'])).project(Arel.sql('*')) + ArelArray.find_by_sql(query.to_sql).should include(one) + + query = arel_table.where(arel_table[:tags].array_overlap(['two'])).project(Arel.sql('*')) + ArelArray.find_by_sql(query.to_sql).should include(two) + + query = arel_table.where(arel_table[:tags].array_overlap(['two','one'])).project(Arel.sql('*')) + ArelArray.find_by_sql(query.to_sql).should include(two) + ArelArray.find_by_sql(query.to_sql).should include(one) end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index f21441b..e8673f2 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -5,6 +5,7 @@ require 'rspec/rails' require 'rspec/autorun' require 'bourne' +require 'debugger' unless ENV['CI'] || (RUBY_PLATFORM =~ /java/) ENGINE_RAILS_ROOT=File.join(File.dirname(__FILE__), '../')