红宝石。 Mongoid。关系

时间:2012-03-18 17:59:53

标签: ruby mongoid bson database-relations


我遇到过MongoID的一些问题。我有三种型号:

require 'mongoid'

class Configuration
  include Mongoid::Document

  belongs_to :user

  field :links, :type => Array
  field :root, :type => String
  field :objects, :type => Array
  field :categories, :type => Array

  has_many :entries
end

class TimeDim
  include Mongoid::Document

  field :day, :type => Integer
  field :month, :type => Integer
  field :year, :type => Integer
  field :day_of_week, :type => Integer
  field :minute, :type => Integer
  field :hour, :type => Integer

  has_many :entries 
end

class Entry
  include Mongoid::Document

  belongs_to :configuration
  belongs_to :time_dim

  field :category, :type => String

  # any other dynamic fields
end

ConfigurationTimeDim创建文档是成功的。但是当我试图执行以下代码时:

params = Hash.new
params[:configuration] = config # an instance of Configuration from DB
entry.each do |key, value|
  params[key.to_sym] = value # String
end
unless Entry.exists?(conditions: params)
  params[:time_dim] = self.generate_time_dim # an instance of TimeDim from DB
  params[:category] = self.detect_category(descr) # String
  Entry.new(params).save
end

...我看到了以下输出:

/home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/bson-1.6.1/lib/bson/bson_c.rb:24:in `serialize': Cannot serialize an object of class Configuration into BSON. (BSON::InvalidDocument)
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/bson-1.6.1/lib/bson/bson_c.rb:24:in `serialize'
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongo-1.6.1/lib/mongo/cursor.rb:604:in `construct_query_message'
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongo-1.6.1/lib/mongo/cursor.rb:465:in `send_initial_query'
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongo-1.6.1/lib/mongo/cursor.rb:458:in `refresh'
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongo-1.6.1/lib/mongo/cursor.rb:128:in `next'
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongo-1.6.1/lib/mongo/db.rb:509:in `command'
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongo-1.6.1/lib/mongo/cursor.rb:191:in `count'
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/cursor.rb:42:in `block in count'
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/collections/retry.rb:29:in `retry_on_connection_failure'
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/cursor.rb:41:in `count'
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/contexts/mongo.rb:93:in `count'
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/criteria.rb:45:in `count'
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/finders.rb:60:in `exists?'
    from /home/scepion1d/Workspace/RubyMine/dana-x/crawler/crawler.rb:110:in `block (2 levels) in push_entries_to_db'
    from /home/scepion1d/Workspace/RubyMine/dana-x/crawler/crawler.rb:103:in `each'
    from /home/scepion1d/Workspace/RubyMine/dana-x/crawler/crawler.rb:103:in `block in push_entries_to_db'
    from /home/scepion1d/Workspace/RubyMine/dana-x/crawler/crawler.rb:102:in `each'
    from /home/scepion1d/Workspace/RubyMine/dana-x/crawler/crawler.rb:102:in `push_entries_to_db'
    from main_starter.rb:15:in `<main>'

谁能说出我做错了什么?

1 个答案:

答案 0 :(得分:1)

Mongoid不支持使用Mongoid :: Document进行质量分配。你需要一直通过一些哈希。

如果您在此关系中定义accepts_nested_attributes_for,则可以使用attr_attributes参数覆盖它。

此行为与ActiveRecord相同。