Skip to content

Instantly share code, notes, and snippets.

@andykent
Created January 25, 2011 11:14
Show Gist options
  • Save andykent/794799 to your computer and use it in GitHub Desktop.
Save andykent/794799 to your computer and use it in GitHub Desktop.
Demonstrates a bug in mongoid many-to-many associations with string keys
class User
include Mongoid::Document
field :name
references_and_referenced_in_many :accounts
end
class Account
include Mongoid::Document
field :name
key :name
references_and_referenced_in_many :users
end
user = User.create!(:name => 'andy')
# This screws up and sets the account key in the user to an ObjectID
# so you end up with the user looking like..
# "account_ids" : ["4d3eac6770e81283be000068"]
user.accounts.create!(:name => 'test')
# Doing it this way works as expected
# so the user ends up with something like...
# "account_ids" : ["test"]
account = Account.create!(:name => 'test')
account.users << user
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment