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 6061059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:53:44+00:00 2026-05-23T08:53:44+00:00

Moingoid doesn’t seem to be setting embedded relationships persistently during my tests. In my

  • 0

Moingoid doesn’t seem to be setting embedded relationships persistently during my tests. In my user model I have:

  def vote_on(bill, value)
    if my_groups = self.groups
      my_groups.each do |g|
        bill.votes.create(:value => value, :user_id => self.id, :group_id => g.id)
        # result only with factories: bill.votes.first.group = nil
        # and bill.votes.first.user = nil !!
        # self.id and g.id have good values during the test, they just aren't persisting
      end
    else
      raise "no groups for this user" # #{self.full_name}"
    end
  end

Other helpful code might be:

## bill model
class Bill
   embeds_many :votes

## vote model

class Vote
  include Mongoid::Document
  field :value, :type => Symbol # can be :aye, :nay, :abstain
  #field :group_type, :type => Integer

  belongs_to :user
  belongs_to :group

  embedded_in :bill

end

## test

  test "descriptive tally should work" do
    user1 = Factory.build(:user)
    b = Factory.build(:bill)
    user1.vote_on(b, :aye) # nil values created here!
    tally = b.descriptive_tally
    assert_not_nil tally
  end

## bill factory

Factory.define :bill do |f|
  f.bill_html "just the facts"
  ...
  f.state "Introduced"
  f.text_updated_on DateTime.parse("2011-06-16 00:00:00 Z")
  f.text_word_count 2356
  f.votes
end

## user factory

Factory.define :user do |u|
   u.email      'user@domain.com'
   u.name       'user'
   u.roles_mask 1
   u.password   "secret"
   u.password_confirmation "secret"
   u.groups {[Factory.build(:group, {:name => 'foreign', :type => :custom})]}
end

This is a real head-scratcher for me. Perhaps this is a bug I need to better explore and submit. My first guess is that I am just missing something simple in my factories or test setup. This code works well on development. Any help is greatly appreciated.

  • 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-05-23T08:53:44+00:00Added an answer on May 23, 2026 at 8:53 am

    I think there are issues with most factory gems and Mongoid. I seem to recall having a lot of issues trying to use both Factory Girl and Machinist.

    Fabrication seems to be the current recommended standard for doing object generation in tests with Mongoid. It’s worked perfectly for us on all our Mongoid projects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Mongoid and have a project and a user model. in the
In this simplified scenario, I have a model called Post Each post has a
So I have an observer which looks like this: class RecipeObserver < Mongoid::Observer def
I have recently been getting my feet wet in MongoDB using Mongoid w/ Rails
I'd like to use the addToSet method of MongoDB, but Mongoid doesn't currently support
I have 2 classes in Mongoid: Topic & Post. Topic references many posts. I
I have several gems including ruby-debug in a bundler group called :development. When I
So I'm building an app using PHP and MongoDB which will have a fair
Using Mongoid. Unfortunately, Mongoid does not allow for selecting unique / distinct! Have gotten
I want to model e-commerce product attributes that can be changed in run-time. So

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.