Module: ActiveRecordExtended::WhereChain
- Included in:
- ActiveRecord::QueryMethods::WhereChain
- Defined in:
- lib/active_record_extended/query_methods/where_chain.rb
Instance Method Summary collapse
-
#all(opts, *rest) ⇒ Object
Finds Records that contain a single matchable array element User.where.all(tags: 3) # SELECT user.* FROM user WHERE 3 = ALL(user.tags).
-
#any(opts, *rest) ⇒ Object
Finds Records that contain an element in an array column User.where.any(tags: 3) # SELECT user.* FROM user WHERE 3 = ANY(user.tags).
- #contained_within(opts, *rest) ⇒ Object
- #contained_within_or_equals(opts, *rest) ⇒ Object
-
#contains(opts, *rest) ⇒ Object
Finds Records that contains a nested set elements.
- #contains_or_equals(opts, *rest) ⇒ Object
-
#overlap(opts, *rest) ⇒ Object
Finds Records that have an array column that contain any a set of values User.where.overlap(tags: [1,2]) # SELECT * FROM users WHERE tags && 1,2.
Instance Method Details
#all(opts, *rest) ⇒ Object
Finds Records that contain a single matchable array element User.where.all(tags: 3)
# SELECT user.* FROM user WHERE 3 = ALL(user.tags)
34 35 36 |
# File 'lib/active_record_extended/query_methods/where_chain.rb', line 34 def all(opts, *rest) equality_to_function("ALL", opts, rest) end |
#any(opts, *rest) ⇒ Object
Finds Records that contain an element in an array column User.where.any(tags: 3)
# SELECT user.* FROM user WHERE 3 = ANY(user.tags)
27 28 29 |
# File 'lib/active_record_extended/query_methods/where_chain.rb', line 27 def any(opts, *rest) equality_to_function("ANY", opts, rest) end |
#contained_within(opts, *rest) ⇒ Object
12 13 14 |
# File 'lib/active_record_extended/query_methods/where_chain.rb', line 12 def contained_within(opts, *rest) substitute_comparisons(opts, rest, Arel::Nodes::ContainedWithin, "contained_within") end |
#contained_within_or_equals(opts, *rest) ⇒ Object
16 17 18 |
# File 'lib/active_record_extended/query_methods/where_chain.rb', line 16 def contained_within_or_equals(opts, *rest) substitute_comparisons(opts, rest, Arel::Nodes::ContainedWithinEquals, "contained_within_or_equals") end |
#contains(opts, *rest) ⇒ Object
Finds Records that contains a nested set elements
Array Column Type:
User.where.contains(tags: [1, 3])
# SELECT user.* FROM user WHERE user.tags @> {1,3}
HStore Column Type:
User.where.contains(data: { nickname: 'chainer' })
# SELECT user.* FROM user WHERE user.data @> 'nickname' => 'chainer'
JSONB Column Type:
User.where.contains(data: { nickname: 'chainer' })
# SELECT user.* FROM user WHERE user.data @> {'nickname': 'chainer'}
This can also be used along side joined tables
JSONB Column Type Example:
Tag.joins(:user).where.contains(user: { data: { nickname: 'chainer' } })
# SELECT tags.* FROM tags INNER JOIN user on user.id = tags.user_id WHERE user.data @> { nickname: 'chainer' }
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/active_record_extended/query_methods/where_chain.rb', line 58 def contains(opts, *rest) build_where_chain(opts, rest) do |arel| case arel when Arel::Nodes::In, Arel::Nodes::Equality column = left_column(arel) || column_from_association(arel) if %i[hstore jsonb].include?(column.type) Arel::Nodes::ContainsHStore.new(arel.left, arel.right) elsif column.try(:array) Arel::Nodes::ContainsArray.new(arel.left, arel.right) else raise ArgumentError, "Invalid argument for .where.contains(), got #{arel.class}" end else raise ArgumentError, "Invalid argument for .where.contains(), got #{arel.class}" end end end |
#contains_or_equals(opts, *rest) ⇒ Object
20 21 22 |
# File 'lib/active_record_extended/query_methods/where_chain.rb', line 20 def contains_or_equals(opts, *rest) substitute_comparisons(opts, rest, Arel::Nodes::ContainsEquals, "contains_or_equals") end |
#overlap(opts, *rest) ⇒ Object
Finds Records that have an array column that contain any a set of values User.where.overlap(tags: [1,2])
# SELECT * FROM users WHERE tags && {1,2}
8 9 10 |
# File 'lib/active_record_extended/query_methods/where_chain.rb', line 8 def overlap(opts, *rest) substitute_comparisons(opts, rest, Arel::Nodes::Overlap, "overlap") end |