no implicit conversion of String into Integer error in ruby on rails

1.9k Views Asked by At

I want access each of 1_1 1_2 1_3 from my rails views , Now how i can define each of this in my Odd class to access them ? I get this error no implicit conversion of String into Integer on this line Odd.new(args.fetch("1_1", {}))

Json responce :

{
    "success": 1,
    "results": {
        "1_1": [
            {
                "id": "1976855",
                "home_od": "501.000",
                "draw_od": "17.000",
                "away_od": "1.025",
                "ss": "0:1",
                "time_str": "91",
                "add_time": "1480296195"
            }
],
        "1_2": [
            {
                "id": "1976855",
                "home_od": "501.000",
                "draw_od": "17.000",
                "away_od": "1.025",
                "ss": "0:1",
                "time_str": "91",
                "add_time": "1480296195"
            }
],
        "1_3": [
            {
                "id": "1976855",
                "home_od": "501.000",
                "draw_od": "17.000",
                "away_od": "1.025",
                "ss": "0:1",
                "time_str": "91",
                "add_time": "1480296195"
            }
]
}
}

odd model

module Bapi
  class Odd < Base

    attr_accessor :event_id,
                  :full_time

   CACHE_DEFAULTS = { expires_in: 7.day, force: false }
   def self.find(query = {})
      cache = CACHE_DEFAULTS
      response = Request.where("/v1/event/odds",CACHE_DEFAULTS, query)
      inplaies = response.fetch('results',[]).map { |odd| Odd.new(odd) }
      inplaies
    end


    def initialize(args = {})
      super(args)
      self.full_time =  parse_1x2(args)
    end

    def parse_1x2(args = {})
      Odd.new(args.fetch("1_1", {}))
    end

  end
end
1

There are 1 best solutions below

0
On BEST ANSWER

When you do

inplaies = response.fetch('results',[]).map { |odd| Odd.new(odd) }

You are saying get me all of the 'results' of response, which are

{
   "1_1": [{}], 
   "1_2": [{}],
 }

So far so good. You then call .map on this. When you call .map, it converts each element of that hash in to a 2 element array, with the first element as the key, and the second element as the value. So your variables 'odd' now look like this:

["1_1", [{"id"=>"1976855", "home_od"=>"501.000", "draw_od"=>"17.000", "away_od"=>"1.025", "ss"=>"0:1", "time_str"=>"91", "add_time"=>"1480296195"}]]
["1_2", [{"id"=>"1976855", "home_od"=>"501.000", "draw_od"=>"17.000", "away_od"=>"1.025", "ss"=>"0:1", "time_str"=>"91", "add_time"=>"1480296195"}]]
["1_3", [{"id"=>"1976855", "home_od"=>"501.000", "draw_od"=>"17.000", "away_od"=>"1.025", "ss"=>"0:1", "time_str"=>"91", "add_time"=>"1480296195"}]]

Then you are passing each of these array elements to Odd.new, so you're effectively doing:

Odd.new(["1_1", [{"id"=>"1976855", "home_od"=>"501.000", "draw_od"=>"17.000", "away_od"=>"1.025", "ss"=>"0:1", "time_str"=>"91", "add_time"=>"1480296195"}]])

As I mentioned in my comment, your Odd initializer expects a hash, but you are supplying this Array instead.

Finally it calls

parse_1x2(args)

Which with your arguments, is

parse_1x2(["1_1", [{"id"=>"1976855", "home_od"=>"501.000", "draw_od"=>"17.000", "away_od"=>"1.025", "ss"=>"0:1", "time_str"=>"91", "add_time"=>"1480296195"}]])

But this method expects a hash.

When you call .fetch on an Array, it expects you to supply a numeric index. But you are calling .fetch with "1_1", which is a string, because you expect "args" to be a hash right there.

You can see this in action by firing up Rails console and doing:

[1,2,3].fetch(0) # will work
[1,2,3].fetch("some string key") # will fail with the same error you're getting

What you want to do here to get off the ground and running is convert your 2-element array in to a hash again:

Change

inplaies = response.fetch('results',[]).map { |odd| Odd.new(odd) }

To

inplaies = response.fetch('results',[]).map { |odd| Odd.new([odd].to_h) }

With this you are converting your 2 element array back in to a Hash, which is what you expect later on in the code.