久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

  • <small id='gwB9a'></small><noframes id='gwB9a'>

    <tfoot id='gwB9a'></tfoot>
    <legend id='gwB9a'><style id='gwB9a'><dir id='gwB9a'><q id='gwB9a'></q></dir></style></legend>

      • <bdo id='gwB9a'></bdo><ul id='gwB9a'></ul>
      <i id='gwB9a'><tr id='gwB9a'><dt id='gwB9a'><q id='gwB9a'><span id='gwB9a'><b id='gwB9a'><form id='gwB9a'><ins id='gwB9a'></ins><ul id='gwB9a'></ul><sub id='gwB9a'></sub></form><legend id='gwB9a'></legend><bdo id='gwB9a'><pre id='gwB9a'><center id='gwB9a'></center></pre></bdo></b><th id='gwB9a'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='gwB9a'><tfoot id='gwB9a'></tfoot><dl id='gwB9a'><fieldset id='gwB9a'></fieldset></dl></div>

        使用 Rails 將外部 JSON 保存到 DB

        Saving external JSON to DB with Rails(使用 Rails 將外部 JSON 保存到 DB)
        <i id='O96yX'><tr id='O96yX'><dt id='O96yX'><q id='O96yX'><span id='O96yX'><b id='O96yX'><form id='O96yX'><ins id='O96yX'></ins><ul id='O96yX'></ul><sub id='O96yX'></sub></form><legend id='O96yX'></legend><bdo id='O96yX'><pre id='O96yX'><center id='O96yX'></center></pre></bdo></b><th id='O96yX'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='O96yX'><tfoot id='O96yX'></tfoot><dl id='O96yX'><fieldset id='O96yX'></fieldset></dl></div>
          <bdo id='O96yX'></bdo><ul id='O96yX'></ul>
          <tfoot id='O96yX'></tfoot>
          <legend id='O96yX'><style id='O96yX'><dir id='O96yX'><q id='O96yX'></q></dir></style></legend>

              <tbody id='O96yX'></tbody>

              • <small id='O96yX'></small><noframes id='O96yX'>

                1. 本文介紹了使用 Rails 將外部 JSON 保存到 DB的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我正在使用 gem 'httparty' 對(duì)外部源進(jìn)行 GET 調(diào)用;這是我的 Controller.rb:

                  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
                  

                  這會(huì)在我的 rails 控制臺(tái)中返回正確的輸出,但現(xiàn)在的問題是如何將 ['id'] 保存到我的數(shù)據(jù)庫中?

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

                  目前,我的 User 模型有 :id 和 :name;來自外部 API 的 JSON 對(duì)象發(fā)送 :id 和 :name 以及一堆我不需要的其他信息.

                  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
                  

                  感謝任何幫助,謝謝!

                  推薦答案

                  首先,我建議您為 id 創(chuàng)建另一列(例如 external_id 或其他內(nèi)容),而不是將其保存在User 模型的實(shí)際 id 列(該列在 ActiveRecord 中非常重要,您真的不想直接設(shè)置它).您可以驗(yàn)證該列的唯一性,以確保不會(huì)將相同的用戶數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫中的多個(gè)單獨(dú)記錄中.

                  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.

                  擁有該列后,在您的 User 模型中創(chuàng)建一個(gè)類方法(我已將其稱為 save_data_from_api)以將 JSON 數(shù)據(jù)加載到數(shù)據(jù)庫中:

                  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
                  

                  這是做什么的:

                  • 將 JSON 數(shù)據(jù)映射到一個(gè)塊 ??(user_data.map),它接受每個(gè) JSON 用戶和
                  • Map the JSON data to a block (user_data.map) which takes each JSON user and
                  1. 初始化一個(gè)新用戶 (User.new)
                  2. 為其分配 id JSON 數(shù)據(jù) (line['user']['id'])
                  3. 保存新用戶(u.save),并且
                  4. 返回它(塊中的最后一個(gè) u).
                  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).

                2. 然后,獲取結(jié)果(分配給 users)并僅選擇數(shù)據(jù)庫中實(shí)際存在(被持久化)的結(jié)果 (users.select(&:persisted?)).例如,如果您對(duì) external_id 有唯一性約束,并且您嘗試再次加載 db 數(shù)據(jù),u.save 將返回 false,不會(huì)(重新)創(chuàng)建記錄,并且這些結(jié)果將從方法返回的結(jié)果中過濾掉.
                3. 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.
                4. 這可能是也可能不是您想要的返回值.此外,您可能希望在塊中添加更多屬性分配(name 等來自 JSON 數(shù)據(jù)).我讓你來填寫其他細(xì)節(jié).

                  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.

                  這篇關(guān)于使用 Rails 將外部 JSON 保存到 DB的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  Apache Nifi How to load JSON with nested array JSON and Call Oracle Stored Procedure(Apache Nifi 如何使用嵌套數(shù)組 JSON 加載 JSON 并調(diào)用 Oracle 存儲(chǔ)過程)
                  covertJSONtoSQL returning empty values in NiFi(covertJSONtoSQL 在 NiFi 中返回空值)
                  Is there any way to use json objects in SQL(有沒有辦法在SQL中使用json對(duì)象)
                  How to access default Rails sqlite db?(如何訪問默認(rèn)的 Rails sqlite 數(shù)據(jù)庫?)
                  Python: Django: how to get order_detail_data inside order_data as per order_id?(Python:Django:如何根據(jù) order_id 在 order_data 中獲取 order_detail_data?)
                  Consuming web service and inserting CLOB using Node.js to Oracle Database table(使用 Web 服務(wù)并使用 Node.js 將 CLOB 插入 Oracle 數(shù)據(jù)庫表)
                    <legend id='0Lhn3'><style id='0Lhn3'><dir id='0Lhn3'><q id='0Lhn3'></q></dir></style></legend>
                      <bdo id='0Lhn3'></bdo><ul id='0Lhn3'></ul>

                          <tfoot id='0Lhn3'></tfoot>
                        • <small id='0Lhn3'></small><noframes id='0Lhn3'>

                          <i id='0Lhn3'><tr id='0Lhn3'><dt id='0Lhn3'><q id='0Lhn3'><span id='0Lhn3'><b id='0Lhn3'><form id='0Lhn3'><ins id='0Lhn3'></ins><ul id='0Lhn3'></ul><sub id='0Lhn3'></sub></form><legend id='0Lhn3'></legend><bdo id='0Lhn3'><pre id='0Lhn3'><center id='0Lhn3'></center></pre></bdo></b><th id='0Lhn3'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='0Lhn3'><tfoot id='0Lhn3'></tfoot><dl id='0Lhn3'><fieldset id='0Lhn3'></fieldset></dl></div>

                              <tbody id='0Lhn3'></tbody>
                            主站蜘蛛池模板: 日韩中文字幕在线观看视频 | 亚洲区视频| 中文字幕视频在线看5 | 欧美日韩精品一区二区 | 在线国产一区二区 | 日韩蜜桃视频 | 国产福利资源在线 | 亚洲一区二区三区免费观看 | 理论片87福利理论电影 | 欧美黑人狂野猛交老妇 | 日韩在线综合网 | 中文字幕在线观看视频一区 | 久色网| 欧美激情视频一区二区三区在线播放 | 国产精品成人一区 | 91av视频在线免费观看 | 国产九九av | 亚洲av毛片成人精品 | 亚洲九九 | 欧美成人一区二免费视频软件 | www.99热这里只有精品 | 亚洲一区二区免费视频 | 亚洲69p | 尤物在线精品视频 | 美国一级毛片a | 一级亚洲| 久久国产精品一区 | 亚洲精品www | 久久久久国产精品一区 | 国产日韩精品一区 | 免费在线观看av的网站 | 99re在线播放 | 亚洲欧美日韩精品久久亚洲区 | 国产精品毛片一区二区在线看 | 国产精品免费一区二区 | 国家一级黄色片 | 午夜精品久久久久久久久久久久 | 久久精品99 | 亚洲精品久久久 | 永久av| 久草免费在线 |