Saving external JSON to DB with Rails

前端 未结 2 462
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-15 00:50

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.         


        
相关标签:
2条回答
  • 2020-12-15 01:10

    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
    
    0 讨论(0)
  • 2020-12-15 01:12

    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.

    0 讨论(0)
提交回复
热议问题