Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

where by array|range attribute with array or range value #26074

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 11 additions & 7 deletions activerecord/lib/active_record/relation/predicate_builder.rb
Expand Up @@ -87,14 +87,14 @@ def create_binds_for_hash(attributes)
binds = []

attributes.each do |column_name, value|
case value
when Hash
case
when value.is_a?(Hash)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@esparta Thank you for your suggestion. I changed to keep the case statement. Thanks!

attrs, bvs = associated_predicate_builder(column_name).create_binds_for_hash(value)
result[column_name] = attrs
binds += bvs
when Relation
when value.is_a?(Relation)
binds += value.bound_attributes
when Range
when value.is_a?(Range) && !table.type(column_name).respond_to?(:subtype)
first = value.begin
last = value.end
unless first.respond_to?(:infinite?) && first.infinite?
Expand Down Expand Up @@ -155,9 +155,13 @@ def handler_for(object)
end

def can_be_bound?(column_name, value)
!value.nil? &&
handler_for(value).is_a?(BasicObjectHandler) &&
!table.associated_with?(column_name)
return if table.associated_with?(column_name)
case value
when Array, Range
table.type(column_name).respond_to?(:subtype)
else
!value.nil? && handler_for(value).is_a?(BasicObjectHandler)
end
end

def build_bind_param(column_name, value)
Expand Down
6 changes: 6 additions & 0 deletions activerecord/test/cases/adapters/postgresql/array_test.rb
Expand Up @@ -290,6 +290,12 @@ def test_assigning_valid_pg_array_literal
assert_equal record.tags, record.reload.tags
end

def test_where_by_attribute_with_array
tags = ["black", "blue"]
record = PgArray.create!(tags: tags)
assert_equal record, PgArray.where(tags: tags).take
end

def test_uniqueness_validation
klass = Class.new(PgArray) do
validates_uniqueness_of :tags
Expand Down
6 changes: 6 additions & 0 deletions activerecord/test/cases/adapters/postgresql/range_test.rb
Expand Up @@ -282,6 +282,12 @@ def test_exclude_beginning_for_subtypes_without_succ_method_is_not_supported
assert_raises(ArgumentError) { PostgresqlRange.create!(tstz_range: "(''2010-01-01 14:30:00+05'', ''2011-01-01 14:30:00-03'']") }
end

def test_where_by_attribute_with_range
range = 1..100
record = PostgresqlRange.create!(int4_range: range)
assert_equal record, PostgresqlRange.where(int4_range: range).take
end

def test_update_all_with_ranges
PostgresqlRange.create!

Expand Down