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
When you do
You are saying get me all of the 'results' of response, which are
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:
Then you are passing each of these array elements to Odd.new, so you're effectively doing:
As I mentioned in my comment, your Odd initializer expects a hash, but you are supplying this Array instead.
Finally it calls
Which with your arguments, is
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:
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
To
With this you are converting your 2 element array back in to a Hash, which is what you expect later on in the code.