Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7905323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:26:09+00:00 2026-06-03T10:26:09+00:00

I’m trying to follow along with http://mongotips.com/b/array-keys-allow-for-modeling-simplicity/ I have a Story document and a

  • 0

I’m trying to follow along with http://mongotips.com/b/array-keys-allow-for-modeling-simplicity/

I have a Story document and a Rating document. The user will rate a story, so I wanted to create a many relationship to ratings by users as such:

class StoryRating
  include MongoMapper::Document

  # key <name>, <type>
  key :user_id, ObjectId
  key :rating, Integer
  timestamps!

end

class Story
  include MongoMapper::Document

  # key <name>, <type>
  timestamps!
  key :title, String
  key :ratings, Array, :index => true

  many :story_ratings, :in => :ratings

end

Then

irb(main):006:0> s = Story.create  
irb(main):008:0> s.ratings.push(Rating.new(user_id: '0923ksjdfkjas'))  
irb(main):009:0> s.ratings.last.save  
=> true  
irb(main):010:0> s.save  
BSON::InvalidDocument: Cannot serialize an object of class StoryRating into BSON.  
    from /usr/local/lib/ruby/gems/1.9.1/gems/bson-1.6.2/lib/bson/bson_c.rb:24:in `serialize' (...)

Why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T10:26:13+00:00Added an answer on June 3, 2026 at 10:26 am

    You should be using the association “story_rating” method for your push/append rather than the internal “rating” Array.push to get what you want to follow John Nunemaker’s “Array Keys Allow For Modeling Simplicity” discussion. The difference is that with the association method, MongoMapper will insert the BSON::ObjectId reference into the array, with the latter you are pushing a Ruby StoryRating object into the Array, and the underlying driver driver cant serialize it.

    Here’s a test that works for me, that shows the difference. Hope that this helps.

    Test

    require 'test_helper'
    
    class Object
      def to_pretty_json
        JSON.pretty_generate(JSON.parse(self.to_json))
      end
    end
    
    class StoryTest < ActiveSupport::TestCase
      def setup
        User.delete_all
        Story.delete_all
        StoryRating.delete_all
        @stories_coll = Mongo::Connection.new['free11513_mongomapper_bson_test']['stories']
      end
    
      test "Array Keys" do
        user = User.create(:name => 'Gary')
        story = Story.create(:title => 'A Tale of Two Cities')
        rating = StoryRating.create(:user_id => user.id, :rating => 5)
        assert_equal(1, StoryRating.count)
        story.ratings.push(rating)
        p story.ratings
        assert_raise(BSON::InvalidDocument) { story.save }
        story.ratings.pop
        story.story_ratings.push(rating) # note story.story_ratings, NOT story.ratings
        p story.ratings
        assert_nothing_raised(BSON::InvalidDocument) { story.save }
        assert_equal(1, Story.count)
        puts Story.all(:ratings => rating.id).to_pretty_json
      end
    end
    

    Result

    Run options: --name=test_Array_Keys
    
    # Running tests:
    
    [#<StoryRating _id: BSON::ObjectId('4fa98c25e4d30b9765000003'), created_at: Tue, 08 May 2012 21:12:05 UTC +00:00, rating: 5, updated_at: Tue, 08 May 2012 21:12:05 UTC +00:00, user_id: BSON::ObjectId('4fa98c25e4d30b9765000001')>]
    [BSON::ObjectId('4fa98c25e4d30b9765000003')]
    [
      {
        "created_at": "2012-05-08T21:12:05Z",
        "id": "4fa98c25e4d30b9765000002",
        "ratings": [
          "4fa98c25e4d30b9765000003"
        ],
        "title": "A Tale of Two Cities",
        "updated_at": "2012-05-08T21:12:05Z"
      }
    ]
    .
    
    Finished tests in 0.023377s, 42.7771 tests/s, 171.1084 assertions/s.
    
    1 tests, 4 assertions, 0 failures, 0 errors, 0 skips
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.