Saving external JSON to DB with Rails

前提是你 提交于 2019-11-27 14:05:09

问题


I'm making a GET call to an external source using the gem 'httparty'; here's my 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

This returns the correct output in my rails console, but now the question is how do I save the ['id'] to my db?

Currently, my User model has :id and :name; the JSON object from the external API sends :id and :name along with a bunch of other information I don't need.

class User < ActiveRecord::Base
   attr_accessor :id, :name
end

Any help is appreciated, thanks!


回答1:


First, I would suggest that you create another column for the id (say external_id or something), rather than saving it in the actual id column of the User model (that column is very important in ActiveRecord and you really don't want to be setting it directly). You can validate uniqueness on that column to ensure that you don't import the same user data into multiple separate records in the db.

Once you have that column, create a class method in your User model (I've called mine save_data_from_api) to load the JSON data into the db:

class User < ActiveRecord::Base

  # optional, but probably a good idea
  validates :external_id, :uniqueness => true

  def self.save_data_from_api
    response = HTTParty.get('URI')
    user_data = JSON.parse(response)
    users = user_data.map do |line|
      u = User.new
      u.external_id = line['user']['id']
      # set name value however you want to do that
      u.save
      u
    end
    users.select(&:persisted?)
  end

end

What this does:

  • Map the JSON data to a block (user_data.map) which takes each JSON user and
    1. initializes a new user (User.new)
    2. assigns it the id JSON data (line['user']['id'])
    3. saves the new user (u.save), and
    4. return its it (the last u in the block).
  • Then, take the result (assigned to users) and select only those that actually exist (were persisted) in the db (users.select(&:persisted?)). For example, if you have a uniqueness constraint on external_id and you try to load the db data again, u.save will return false, the record will not be (re-)created, and those results will be filtered out of the results returned from the method.

This may or may not be the return value you want. Also, you probably want to add more attribute assignments (name, etc. from the JSON data) in the block. I leave it up to you to fill in those other details.




回答2:


Here I am assuming that you have created the uri_id field to store the external id and the name field to store the external name in the user table.

Your Controller code -

def show
  response = HTTParty.get('URI')
  JSON.parse(response).each do |item|
    if (item.id != nil && item.name != nil)
       u = User.new(:uri_id => item.id, :name => item.name)
       u.save
    end
  end
end


来源:https://stackoverflow.com/questions/14374695/saving-external-json-to-db-with-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!