您好我正在尝试为runescape项目创建模型。使用api和httparty。我有很多问题。但是这个是关于使用覆盖初始化方法说它有错误的参数数量。
total.
因此在rails控制台中,我运行以下命令来测试它:
item_test = Item.find(" 227")
class Item < ApplicationRecord
require 'json'
include HTTParty
base_uri 'http://services.runescape.com/m=itemdb_oldschool/'
attr_accessor :name, :description, :price, :icon_url
def initialize(name, description, price, icon_url)
super
self.name = name
self.description = description
self.price = price
self.icon_url = icon_url
end
def self.find(name)
response = get("/api/catalogue/detail.json?item=#{name}")
if response.success?
parsed = JSON.parse(response)
self.new(
parsed["item"]["name"],
parsed["item"]["description"],
parsed["item"]["current"]["price"],
parsed["item"]["icon_large"]
)
else
# this just raises the net/http response that was raised
raise response.response
end
end
end
但初始化需要4个参数。当它减少为1时,它说它需要4.这让我非常困惑。
我甚至不确定覆盖基本初始化是做这样的事情的前进方向。所以,如果您有更好的想法,请告诉我我是新手。
总之,我的问题是为什么我的对象没有被正确创建?其次,这是创建对象的正确方法吗?