【问题标题】:Ruby. Mongoid. Relations红宝石。蒙古族。关系
【发布时间】:2012-04-03 09:11:39
【问题描述】:


我在使用 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

Configurations 和TimeDims 创建文档成功。但是当我尝试执行以下代码时:

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>'

谁能告诉我我做错了什么?

【问题讨论】:

  • 您是否尝试过使用各自的 id 设置 params[:configuration_id]params[:time_dim_id],而不是使用整个模型对象?
  • 谢谢,它有效。但我认为 mongoid 支持工作对象。

标签: ruby mongoid bson database-relations


【解决方案1】:

Mongoid 不支持 Mongoid::Document 的 Mass 赋值。你需要一直通过一些哈希。

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

此行为与 ActiveRecord 相同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多