Created
January 25, 2011 11:14
-
-
Save andykent/794799 to your computer and use it in GitHub Desktop.
Demonstrates a bug in mongoid many-to-many associations with string keys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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