I'm getting this error when ActiveResource is attempting to decode a response from an API:
ArgumentError (expected attributes to be able to convert to Hash, got ["menu_items", ...
Important gem versions:
- Rails: 5.1.6
- ActiveResource: 5.0.0
Here's my API calling model:
class Menu < ActiveResource::Base
self.site = "https://blah.asd.com"
self.prefix = "/api/"
self.element_name = "menu"
has_many :menu_items
has_many :ad_units
def self.collection_name
element_name
end
end
And here are the children models:
class AdUnit < ActiveResource::Base
self.site = "https://blah.asd.com"
self.prefix = "/api/"
self.element_name = "ad_unit"
belongs_to :menu
end
class MenuItem < ActiveResource::Base
self.site = "https://blah.asd.com"
self.prefix = "/api/"
self.element_name = "menu_item"
belongs_to :menu
end
And finally, here's the json from the api:
{
"menu_items":[
{
"id":17,
"name":"Turkey Bacon Avocado Sandwich",
"price":5.99,
"original_price":9.99,
"description":"Roast turkey, bacon, swiss, lettuce, tomato, avocado & pesto mayo on toasted whole wheat.",
"restaurant_id":25
},
{
"id":13,
"name":"Wild Salmon",
"price":9.99,
"original_price":13.99,
"description":"Wild fresh herbed seasoned salmon prepared daily with tzatziki sauce and served with fresh beets & onion and roasted veggies.",
"restaurant_id":25
},
{
"id":16,
"name":"BLT+",
"price":9.99,
"original_price":9.99,
"description":"Loaded with crispy bacon, leaf lettuce, sliced tomato, provolone & avocado on panini.",
"restaurant_id":25
},
{
"id":9,
"name":"Indian Chicken Curry",
"price":11.99,
"original_price":11.99,
"description":"Chicken cooked in a mild sauce.",
"restaurant_id":24
},
{
"id":10,
"name":"Indian Goat Curry",
"price":12.99,
"original_price":12.99,
"description":"Goat cooked in a mild sauce.",
"restaurant_id":24
},
{
"id":11,
"name":"Indian Curry Combo",
"price":12.99,
"original_price":12.99,
"description":"Combination of Goat and Chicken Curry, cooked in a mild sauce. Served with rice & naan",
"restaurant_id":24
},
{
"id":12,
"name":"Indian Goat & Chicken Combo",
"price":12.99,
"original_price":12.99,
"description":"Combination of Goat Curry & Butter Chicken. Served with rice and naan.",
"restaurant_id":24
}
],
"ad_units":[
{
"id":3,
"ad_unit_id":2,
"position":2,
"name":"This is a great ad",
"description":"This is a description of an ad unit"
}
]
}
I'd expect this response to be converted into the appropriate ActiveResource objects, but for some reason it's throwing an exception.
Update #1:
I've overridden the load(attributes, remove_root = false, persisted = false) method in my Menu model with the code in the gem repositoy. Here's what the attributes object looks like:
menu_items
{"id"=>17, "name"=>"Turkey Bacon Avocado Sandwich", "price"=>5.99, "original_price"=>9.99, "description"=>"Roast turkey, bacon, swiss, lettuce, tomato, avocado & pesto mayo on toasted whole wheat.", "restaurant_id"=>25}
{"id"=>13, "name"=>"Wild Salmon", "price"=>9.99, "original_price"=>13.99, "description"=>"Wild fresh herbed seasoned salmon prepared daily with tzatziki sauce and served with fresh beets & onion and roasted veggies.", "restaurant_id"=>25}
{"id"=>16, "name"=>"BLT+", "price"=>9.99, "original_price"=>9.99, "description"=>"Loaded with crispy bacon, leaf lettuce, sliced tomato, provolone & avocado on panini.", "restaurant_id"=>25}
{"id"=>9, "name"=>"Indian Chicken Curry", "price"=>11.99, "original_price"=>11.99, "description"=>"Chicken cooked in a mild sauce.", "restaurant_id"=>24}
{"id"=>10, "name"=>"Indian Goat Curry", "price"=>12.99, "original_price"=>12.99, "description"=>"Goat cooked in a mild sauce.", "restaurant_id"=>24}
{"id"=>11, "name"=>"Indian Curry Combo", "price"=>12.99, "original_price"=>12.99, "description"=>"Combination of Goat and Chicken Curry, cooked in a mild sauce. Served with rice & naan", "restaurant_id"=>24}
{"id"=>12, "name"=>"Indian Goat & Chicken Combo", "price"=>12.99, "original_price"=>12.99, "description"=>"Combination of Goat Curry & Butter Chicken. Served with rice and naan.", "restaurant_id"=>24}
Update #2:
It appears no matter what I send, activeresource can't seem to decode it. Even {"nothing": "nothing"} gets the same error.
ArgumentError (expected attributes to be able to convert to Hash, got ["nothing", "nothing"]):
Maybe I'm missing some formatting library? I've removed ActiveRecords from my project which required me to remove require 'rails/all' from my application.rb file.
I've replaced require 'rails/all' with:
require "active_model/railtie"
# require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "action_cable/engine"
require "sprockets/railtie"
require "rails/test_unit/railtie"