pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

  • <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="d7d7bbv" 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="vtxvnbr" 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的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我正在使用 gem 'httparty' 對外部源進行 GET 調用;這是我的 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
                  

                  這會在我的 rails 控制臺中返回正確的輸出,但現在的問題是如何將 ['id'] 保存到我的數據庫中?

                  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 對象發送 :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 創建另一列(例如 external_id 或其他內容),而不是將其保存在User 模型的實際 id 列(該列在 ActiveRecord 中非常重要,您真的不想直接設置它).您可以驗證該列的唯一性,以確保不會將相同的用戶數據導入到數據庫中的多個單獨記錄中.

                  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 模型中創建一個類方法(我已將其稱為 save_data_from_api)以將 JSON 數據加載到數據庫中:

                  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 數據映射到一個塊 ??(user_data.map),它接受每個 JSON 用戶和
                  • Map the JSON data to a block (user_data.map) which takes each JSON user and
                  1. 初始化一個新用戶 (User.new)
                  2. 為其分配 id JSON 數據 (line['user']['id'])
                  3. 保存新用戶(u.save),并且
                  4. 返回它(塊中的最后一個 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. 然后,獲取結果(分配給 users)并僅選擇數據庫中實際存在(被持久化)的結果 (users.select(&:persisted?)).例如,如果您對 external_id 有唯一性約束,并且您嘗試再次加載 db 數據,u.save 將返回 false,不會(重新)創建記錄,并且這些結果將從方法返回的結果中過濾掉.
                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 數據).我讓你來填寫其他細節.

                  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.

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

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

                  相關文檔推薦

                  Apache Nifi How to load JSON with nested array JSON and Call Oracle Stored Procedure(Apache Nifi 如何使用嵌套數組 JSON 加載 JSON 并調用 Oracle 存儲過程)
                  covertJSONtoSQL returning empty values in NiFi(covertJSONtoSQL 在 NiFi 中返回空值)
                  Is there any way to use json objects in SQL(有沒有辦法在SQL中使用json對象)
                  How to access default Rails sqlite db?(如何訪問默認的 Rails sqlite 數據庫?)
                  Python: Django: how to get order_detail_data inside order_data as per order_id?(Python:Django:如何根據 order_id 在 order_data 中獲取 order_detail_data?)
                  Consuming web service and inserting CLOB using Node.js to Oracle Database table(使用 Web 服務并使用 Node.js 將 CLOB 插入 Oracle 數據庫表)
                    <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="7fdxdfr" id='0Lhn3'><tfoot id='0Lhn3'></tfoot><dl id='0Lhn3'><fieldset id='0Lhn3'></fieldset></dl></div>

                              <tbody id='0Lhn3'></tbody>
                            主站蜘蛛池模板: 外贮压-柜式-悬挂式-七氟丙烷-灭火器-灭火系统-药剂-价格-厂家-IG541-混合气体-贮压-非贮压-超细干粉-自动-灭火装置-气体灭火设备-探火管灭火厂家-东莞汇建消防科技有限公司 | 哈尔滨治「失眠/抑郁/焦虑症/精神心理」专科医院排行榜-京科脑康免费咨询 一对一诊疗 | 宝鸡市人民医院 | 全自动过滤器_反冲洗过滤器_自清洗过滤器_量子除垢环_量子环除垢_量子除垢 - 安士睿(北京)过滤设备有限公司 | 精密五金冲压件_深圳五金冲压厂_钣金加工厂_五金模具加工-诚瑞丰科技股份有限公司 | 阳光1号桔柚_无核沃柑_柑橘新品种枝条苗木批发 - 苧金网 | 阜阳成人高考_阜阳成考报名时间_安徽省成人高考网 | 钢格板|镀锌钢格板|热镀锌钢格板|格栅板|钢格板|钢格栅板|热浸锌钢格板|平台钢格板|镀锌钢格栅板|热镀锌钢格栅板|平台钢格栅板|不锈钢钢格栅板 - 专业钢格板厂家 | 广州办公室设计,办公室装修,写字楼设计,办公室装修公司_德科 | 细沙回收机-尾矿干排脱水筛设备-泥石分离机-建筑垃圾分拣机厂家-青州冠诚重工机械有限公司 | 双能x射线骨密度检测仪_dxa骨密度仪_双能x线骨密度仪_品牌厂家【品源医疗】 | 高温高压釜(氢化反应釜)百科| 烘箱-工业烘箱-工业电炉-实验室干燥箱 - 苏州华洁烘箱制造有限公司 | 盛源真空泵|空压机-浙江盛源空压机制造有限公司-【盛源官网】 | 长沙一级消防工程公司_智能化弱电_机电安装_亮化工程专业施工承包_湖南公共安全工程有限公司 | 超声波清洗机_大型超声波清洗机_工业超声波清洗设备-洁盟清洗设备 | 播音主持培训-中影人教育播音主持学苑「官网」-中国艺考界的贵族学校 | 南方珠江-南方一线电缆-南方珠江科技电缆-南方珠江科技有限公司 南汇8424西瓜_南汇玉菇甜瓜-南汇水蜜桃价格 | 北京亦庄厂房出租_经开区产业园招商信息平台 | 沈阳缠绕膜价格_沈阳拉伸膜厂家_沈阳缠绕膜厂家直销 | 模切之家-专注服务模切行业的B2B平台! | MTK核心板|MTK开发板|MTK模块|4G核心板|4G模块|5G核心板|5G模块|安卓核心板|安卓模块|高通核心板-深圳市新移科技有限公司 | 废旧物资回收公司_广州废旧设备回收_报废设备物资回收-益美工厂设备回收公司 | 定时排水阀/排气阀-仪表三通旋塞阀-直角式脉冲电磁阀-永嘉良科阀门有限公司 | ★塑料拖链__工程拖链__电缆拖链__钢制拖链 - 【上海闵彬】 | 电动打包机_气动打包机_钢带捆扎机_废纸打包机_手动捆扎机 | LINK FASHION 童装·青少年装展 河南卓美创业科技有限公司-河南卓美防雷公司-防雷接地-防雷工程-重庆避雷针-避雷器-防雷检测-避雷带-避雷针-避雷塔、机房防雷、古建筑防雷等-山西防雷公司 | 泰国试管婴儿_泰国第三代试管婴儿_泰国试管婴儿费用/多少钱_孕泰来 | 美名宝起名网-在线宝宝、公司、起名平台 | vr安全体验馆|交通安全|工地安全|禁毒|消防|安全教育体验馆|安全体验教室-贝森德(深圳)科技 | 全自动真空上料机_粉末真空上料机_气动真空上料机-南京奥威环保科技设备有限公司 | 我爱古诗词_古诗词名句赏析学习平台| 颚式破碎机,圆锥破碎机,制砂机-新乡市德诚机电制造有限公司 | 儿童语言障碍训练-武汉优佳加感统文化发展有限公司 | 冷轧机|两肋冷轧机|扁钢冷轧机|倒立式拉丝机|钢筋拔丝机|收线机-巩义市华瑞重工机械制造有限公司 | 河南不锈钢水箱_地埋水箱_镀锌板水箱_消防水箱厂家-河南联固供水设备有限公司 | 5nd音乐网|最新流行歌曲|MP3歌曲免费下载|好听的歌|音乐下载 免费听mp3音乐 | 减速机三参数组合探头|TSM803|壁挂式氧化锆分析仪探头-安徽鹏宸电气有限公司 | 杭州货架订做_组合货架公司_货位式货架_贯通式_重型仓储_工厂货架_货架销售厂家_杭州永诚货架有限公司 | 大通天成企业资质代办_承装修试电力设施许可证_增值电信业务经营许可证_无人机运营合格证_广播电视节目制作许可证 | 发电机组|柴油发电机组-批发,上柴,玉柴,潍柴,康明斯柴油发电机厂家直销 |