Ruby. Mongoid. Relations

Posted by Scepion1d on Stack Overflow See other posts from Stack Overflow or by Scepion1d
Published on 2012-03-18T17:59:52Z Indexed on 2012/03/18 18:02 UTC
Read the original article Hit count: 424

Filed under:
|
|
|


I've encountered some problems with MongoID. I have three models:

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

Creating documents for Configurations and TimeDims is successful. But when i've trying to execute following code:

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

... i saw following output:

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

Can anyone tell what am I doing wrong?

© Stack Overflow or respective owner

Related posts about ruby

Related posts about mongoid