Permalink
Browse files

Update querying doc to include array_contains

  • Loading branch information...
1 parent 2d02582 commit ab3b6e05eb3da33e939b35f993cd0e39869650e6 @jagregory jagregory committed Feb 18, 2013
Showing with 33 additions and 0 deletions.
  1. +33 −0 docs/querying.md
View
@@ -42,6 +42,39 @@ User.where(user_arel[:tags].array_overlap(['one','two']))
# => SELECT \"users\".* FROM \"users\" WHERE \"users\".\"tags\" && '{one,two}'
```
+### @> - Array Contains operator
+
+PostgreSQL has a contains (`@>`) operator for querying whether all the
+elements of an array are within another.
+
+```sql
+ARRAY[1,2,3] @> ARRAY[3,4]
+-- f
+
+ARRAY[1,2,3] @> ARRAY[2,3]
+-- t
+```
+
+Postgres_ext extends the `ActiveRecord::Relation.where` method by
+adding an `array_contains` method. To make a contains query, you
+can do:
+
+```ruby
+User.where.array_contains(:nick_names => ['Bob', 'Fred'])
+```
+
+Postgres_ext defines `array_contains`, an [Arel](https://github.com/rails/arel)
+predicate for the `@>` operator. This is utilized by the
+`where.array_contains` call above.
+
+```ruby
+user_arel = User.arel_table
+
+# Execute the query
+User.where(user_arel[:tags].array_contains(['one','two']))
+# => SELECT "users".* FROM "users" WHERE "users"."tags" @> '{"one","two"}'
+```
+
### ANY or ALL functions
When querying array columns, you have the ability to see if a predicate

0 comments on commit ab3b6e0

Please sign in to comment.