Skip to content

Instantly share code, notes, and snippets.

@dbackeus
Created January 17, 2011 10:58
Show Gist options
  • Save dbackeus/782718 to your computer and use it in GitHub Desktop.
Save dbackeus/782718 to your computer and use it in GitHub Desktop.
Embedded document lose reference to parent when parent fails to save
class Account
include Mongoid::Document
embeds_many :memberships
field :name, :type => String
validates_presence_of :name
end
class Membership
include Mongoid::Document
embedded_in :account, :inverse_of => :memberships
end
a = Account.new :name => "Awesome"
a.memberships.build
a.save
# No problem;
a = Account.first
a.memberships.first.account # #<Account...>
# Fail;
a = Account.first
a.name = ""
a.save # false
a.memberships.first.account # nil
# Fail;
a = Account.first
a.update_attributes :name => "" # false
a.memberships.first.account # nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment