1

一个文件怎么能索赔foo.persisted? == true,什么时候foo.class.count == 0
更重要的是,我怎样才能让文档真正持久化?

更新:从控制器调用Mongo::Foo.create()确实会增加计数。这是意料之中的。从单独的模块/类中调用create, or newand不会增加计数。save

更新:我尝试更改new/save调用create

更新:我也试过@foo_class.with(collection: "mongo_foos").create

[42] pry(#<FoosController>)> foo.class
=> Mongo::Foo
[43] pry(#<FoosController>)> foo.persisted?
=> true
[44] pry(#<FoosController>)> Mongo::Foo.count
=> 0
[47] pry(#<FoosController>)> foo.id
=> "5081a1a786ddc8e035000021"
[48] pry(#<FoosController>)> foo
=> #<Mongo::Foo _id: 5081a1a786ddc8e035000021, _type: nil, foo_daddy_id: nil, old_foo_daddy_id: 219, member_id: "F008AR", unix_time: nil, date_time: 2012-10-19 13:50:54 UTC, submitted: nil, json: nil>

以下是文档类的定义方式:

module Mongo
  class Foo
    include Mongoid::Document

    field :foo_daddy_id
    field :old_foo_daddy_id, type: Integer
    field :member_id,  type: String
    field :unix_time,  type: Integer
    field :date_time,  type: DateTime
    field :submitted,  type: Integer
    field :json,       type: Hash

    belongs_to :foo_daddy, class_name: "Mongo::FooDaddy"
    embeds_many :foo_dumplings, class_name: "Mongo::FooDumpling"

  end
end

该文档正在创建foo.new(params); foo.save

module FooThingy
  module Foo
    class Create < ::FooThingy::Base

      def initialize(options)
        # Sets instance variables used below
      end

      def execute!
        @foo = @foo_class.new({
          date_time: DateTime.strptime(@params["submitted_timestamp"], "%m-%d-%Y %H:%M:%S"),
          member_id: (@params[:member_id].present? ? @params[:member_id] : nil),
          old_foo_daddy_id: @params[:old_foo_daddy_id]
        })

        embed_foo_dumplings

        if @foo.save
          return FooThingy::Result.new(success: true, data: { foo: @foo })
        else
          return FooThingy::Result.new(success: false, data: { foo: @foo })
        end
      end

      private
      def embed_foo_dumplings
        # Embeds foo_dumplings
      end

    end
  end
end

这是我调用该模块来创建文档的地方:

class FoosController < ApplicationController
  def create
    foo_creator = FooThingy::Foo::Create.new(params: params, foo_class: Mongo::Foo)
    foo = foo_creator.execute!
    foo = foo.data[:foo]
    binding.pry
  end
end
4

1 回答 1

0

我认为这个问题并没有看起来那么有趣。我已经修改了 myconfig/mongoid.yml以指定一个新数据库,并且一切都按预期运行。问题可能是由于 32 位 MongoDB 安装的限制。

于 2012-10-19T22:36:34.230 回答