Skip to content

Commit

Permalink
Update querying doc to include array_contains
Browse files Browse the repository at this point in the history
  • Loading branch information
jagregory committed Feb 18, 2013
1 parent 2d02582 commit ab3b6e0
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions docs/querying.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ab3b6e0

Please sign in to comment.