【问题标题】:Is it possible to create a model and child models using Rails + MongoDB/Mongoid with just JSON?是否可以仅使用 JSON 使用 Rails + MongoDB/Mongoid 创建模型和子模型?
【发布时间】:2011-07-10 09:39:18
【问题描述】:

例如,我想从以下 JSON 创建一个新的“Tweet”对象

{:for_user=>14248719, :message=>{:place=>nil, :user=>{:contributors_enabled=>false, :statuses_count=>217, :profile_use_background_image=>true, :friends_count=>3, :profile_background_color=>"C0DEED", :url=>nil, :following=>nil, :verified=>false, :profile_background_image_url=>"http://a3.twimg.com/a/1298748610/images/themes/theme1/bg.png", :time_zone=>nil, :favourites_count=>0, :profile_text_color=>"333333", :follow_request_sent=>nil, :profile_sidebar_fill_color=>"DDEEF6", :description=>"Lets get panda dev team rawk!", :id_str=>"95923128", :profile_background_tile=>false, :followers_count=>2, :created_at=>"Thu Dec 10 15:29:56 +0000 2009", :protected=>true, :profile_image_url=>"http://a2.twimg.com/profile_images/1121266473/panda01_normal.jpg", :is_translator=>false, :show_all_inline_media=>false, :geo_enabled=>false, :profile_link_color=>"0084B4", :location=>"Brighton", :name=>"letsgetpandadev", :listed_count=>0, :notifications=>nil, :profile_sidebar_border_color=>"C0DEED", :screen_name=>"letsgetpandadev", :id=>95923128, :lang=>"en", :utc_offset=>nil}, :favorited=>false, :coordinates=>nil, :text=>"another magic tweet", :in_reply_to_screen_name=>nil, :in_reply_to_user_id=>nil, :in_reply_to_status_id=>nil, :in_reply_to_status_id_str=>nil, :source=>"web", :contributors=>nil, :retweeted=>false, :in_reply_to_user_id_str=>nil, :id_str=>"44709765150015488", :retweet_count=>0, :created_at=>"Mon Mar 07 10:43:33 +0000 2011", :geo=>nil, :id=>44709765150015488, :entities=>{:urls=>[], :user_mentions=>[], :hashtags=>[]}, :truncated=>false}}

..并将“message”、“message.user”等保存为嵌入式子模型。 解析 JSON 并插入它只会产生一个“Tweet”对象,并在消息属性中保存一个哈希。

我想要实现的目标可能吗?如果是这样,怎么做?如果没有,是否可以通过其他方法实现?

我正在为 MongoDB 使用 Rails 3 和 Mongoid gem。

谢谢

【问题讨论】:

  • 这看起来像一个 ruby​​ 哈希而不像 JSON。也许用.to_json 转换它
  • 对不起,是的。我贴错东西了。这是在它从 JSON 解析为 ruby​​ 哈希之后。

标签: ruby-on-rails ruby twitter mongodb mongoid


【解决方案1】:

当然可以——只是不能使用 mongoid/mongomapper 包装器。通过 ruby​​ 驱动程序本身连接...本教程还有更多内容:http://api.mongodb.org/ruby/current/file.TUTORIAL.html

require 'rubygems'
require 'mongo'

db = Mongo::Connection.new.db("mydb")
test_collection = db.collection("testCollection")

doc = {"name" => "MongoDB", "type" => "database", "count" => 1,
       "info" => {"x" => 203, "y" => 102}
      }
test_collection.insert(doc)

【讨论】:

    【解决方案2】:

    如果您希望消息成为它自己的对象,那么您需要嵌入或引用另一个定义它的模型。

    class Tweet
      include Mongoid::Document
    
      embeds_one :message
    end
    
    class Message
      include Mongoid::Document
    
      embedded_in :tweet
    
      field :place
      ...
    end
    

    当您构建属性时,您将希望以与嵌套属性的表单发布相同的方式构建它们,并设置您的模型以这种方式接受它们。

    【讨论】:

    • 好的,谢谢保罗。只是为了确认一下,不可能只插入一团 JSON,我将不得不手动构造对象或至少操作 JSON 以创建对象......即使我设置了“allow_dynamic_fields:true”在我的配置中?
    • 建立这个关联后,一切似乎都正常了。我想我希望这一切都可以是动态的,我不必建立这些关联。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多