我正在使用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
感谢您的帮助!任何帮助都将不胜感激。
id
保存在类似于uri_id
的列中,而不是id
列中?在这种情况下,只需创建新列并在应用程序中使用即可。我认为这将是最好的选择。 - tiktak