1

An active_resource based class:

Contact.search(:email => ['bar@foo.com','foo@bar.com'])

would produce this:

?email[]=bar@foo.com&email[]=foo@bar.com

The specific API that I'm working with requires this:

?email=bar@foo.com&email=foo@bar.com

So playing around I have found that:

ActiveResouce calls:

# Find every resource
find_every(options)

which calls:

  # Builds the query string for the request.
  def query_string(options)
    "?#{options.to_query}" unless options.nil? || options.empty?
  end

So if I update:

class Array
  # Converts an array into a string suitable for use as a URL query string,
  # using the given +key+ as the param name.
  #
  # ['Rails', 'coding'].to_query('hobbies') # => "hobbies%5B%5D=Rails&hobbies%5B%5D=coding"
  def to_query(key)
    prefix = "#{key}[]"
    collect { |value| value.to_query(prefix) }.join '&'
  end
end

to this:

class Array
  # Converts an array into a string suitable for use as a URL query string,
  # using the given +key+ as the param name.
  #
  # ['Rails', 'coding'].to_query('hobbies') # => "hobbies%5B%5D=Rails&hobbies%5B%5D=coding"
  def to_query(key)
    prefix = "#{key}"
    collect { |value| value.to_query(prefix) }.join '&'
  end
end

it works!! however I'm not particularly happy redefining Array.to_param because this may have unforeseen issues, especially as this plug in needs to work within rails.

Is there another way I can patch only my version?

ktec
  • 2,453
  • 3
  • 26
  • 32

2 Answers2

2

I would definitely recommend NOT monkey patching an array method like that. If you only have a single model, could you override the search method?

class Contact
  def self.search(options={})
    super(options).gsub('[]','')
  end
end
Peter Brown
  • 50,956
  • 18
  • 113
  • 146
  • Thanks Beerlington, thanks your reply led me to a much better solution - shown below. – ktec Nov 22 '11 at 14:17
0

As this behaviour is standard through the API I'm using I was able to add this patch to my ActiveRecord::Base class.

  def query_string(options)
    begin
      super(options).gsub('%5B%5D','')
    rescue
    end
  end

Thanks to Beerlington for pointing me in the right direction for this.

ktec
  • 2,453
  • 3
  • 26
  • 32