Рубин. монгоид. связи


У меня возникли проблемы с 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>'

Кто-нибудь может сказать, что я делаю неправильно?


person Недоброе Привидение    schedule 18.03.2012    source источник
comment
Вы пытались установить params[:configuration_id] и params[:time_dim_id] с соответствующими идентификаторами вместо использования всех объектов модели?   -  person rubish    schedule 18.03.2012
comment
Спасибо, это работает. Но я думаю, что монгоиды поддерживают рабочие объекты.   -  person Недоброе Привидение    schedule 19.03.2012


Ответы (1)


Mongoid не поддерживает массовое назначение с Mongoid::Document. Вам нужно все время проходить мимо какого-то Хэша.

Если вы определили accepts_nested_attributes_for для этого отношения, вы можете переопределить его, используя параметры attr_attributes.

Это поведение такое же, как у ActiveRecord.

person shingara    schedule 19.03.2012