ransack: Filtering by scopes with an array as an argument raises ArgumentError

Assuming:

class Person < ActiveRecord::Base
    scope :domestic, ->(countries) { where(country: countries) }
    def self.ransackable_scopes(auth_object=nil); [:domestic]; end
end

Trying to filter by a list of countries:

Person.search(country: ['US', 'JP'])

Will raise ArgumentError: wrong number of arguments (2 for 1)

Again, we are able to format the input in order to avoid this.

Person.search(country: [['US', 'JP']])

This avoids the issue as it seems to be a problem with splatting the args.

About this issue

  • Original URL
  • State: open
  • Created 10 years ago
  • Reactions: 4
  • Comments: 15 (9 by maintainers)

Most upvoted comments

I tried used splat and then flatten the argument as a workaround

class Person < ActiveRecord::Base
    scope :domestic, ->(*countries) do 
       flatten_countries = countries.flatten
       where(country: flatten_countries)
     end
    def self.ransackable_scopes(auth_object=nil); [:domestic]; end
end

Hit the same problem. Does it count as activity? 😄

Closed due to inactivity

How would one get around this? Is there a viable alternative method.