Skip to content

NoMethodError undefined method 'to_i' for an instance of Array #1556

Open
@heaven

Description

@heaven

The error happens at

val.blank? ? nil : val.to_i

The error happens when passing an array instead of a string/int to an _eq predicate.

The implementation of this Value class doesn't handle errors as it should. It rescues things like Date.new(y, m, d) rescue nil, yielding invalid search results eventually.

I would say it has to do exactly the opposite, all those rescue nil should be gone, raising errors when invalid data is submitted. Those errors have to be handled in the application, not in the library.

A new error class can be introduced, e.g., Ransack::InvalidPropertyError. This error can be raised when the submitted property doesn't match the expectations.

def cast_to_integer(val)
  raise Ransack::InvalidPropertyError, "..." unless val.respond_to?(:to_i)
  val.blank? ? nil : val.to_i
end

Date parsing should rely on RFC formats and raise otherwise, rather than trying some magick tricks, rescuing, and silently swallowing the errors, yielding invalid search results.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions