我正在使用 gem 'httparty' 对外部源进行 GET 调用;这是我的 Controller.rb:
def show
response = HTTParty.get('URI')
user = JSON.parse(response)
user.each {|line| puts line['user']['id']}
#the "['user']['id']" is because of the nested JSON object that is returned after the
parse.
end
这会在我的 rails 控制台中返回正确的输出,但现在的问题是如何将 ['id'] 保存到我的数据库中?
目前,我的用户模型有 :id 和 :name; 来自外部 API 的 JSON 对象发送 :id 和 :name 以及一堆我不需要的其他信息。
class User < ActiveRecord::Base
attr_accessor :id, :name
end
任何帮助表示赞赏,谢谢!