Skip to content

Commit

Permalink
Catch the RangeError closer to the cause
Browse files Browse the repository at this point in the history
  • Loading branch information
kamipo committed Jun 24, 2020
1 parent ed06b52 commit 0601a9f
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 9 deletions.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/associations/association.rb
Expand Up @@ -220,7 +220,7 @@ def find_target
end

binds = AssociationScope.get_bind_values(owner, reflection.chain)
sc.execute(binds, klass.connection) { |record| set_inverse_instance(record) } || []
sc.execute(binds, klass.connection) { |record| set_inverse_instance(record) }
end

# The scope for this association.
Expand Down
Expand Up @@ -68,6 +68,8 @@ def select_all(arel, name = nil, binds = [], preparable: nil)
else
select(sql, name, binds)
end
rescue ::RangeError
ActiveRecord::Result.new([], [])
end

# Returns a record hash with the column names as keys and column values
Expand Down
9 changes: 3 additions & 6 deletions activerecord/lib/active_record/core.rb
Expand Up @@ -192,11 +192,8 @@ def find(*ids) # :nodoc:
where(key => params.bind).limit(1)
}

record = statement.execute([id], connection)&.first
unless record
raise RecordNotFound.new("Couldn't find #{name} with '#{key}'=#{id}", name, key, id)
end
record
statement.execute([id], connection).first ||
raise(RecordNotFound.new("Couldn't find #{name} with '#{key}'=#{id}", name, key, id))
end

def find_by(*args) # :nodoc:
Expand Down Expand Up @@ -229,7 +226,7 @@ def find_by(*args) # :nodoc:
}

begin
statement.execute(values, connection)&.first
statement.execute(values, connection).first
rescue TypeError
raise ActiveRecord::StatementInvalid
end
Expand Down
2 changes: 0 additions & 2 deletions activerecord/lib/active_record/statement_cache.rb
Expand Up @@ -148,8 +148,6 @@ def execute(params, connection, &block)
sql = query_builder.sql_for bind_values, connection

klass.find_by_sql(sql, bind_values, preparable: true, &block)
rescue ::RangeError
nil
end

def self.unsupported_value?(value)
Expand Down

0 comments on commit 0601a9f

Please sign in to comment.