Skip to content

Instantly share code, notes, and snippets.

@bernerdschaefer
Created July 23, 2010 15:17
Show Gist options
  • Save bernerdschaefer/487567 to your computer and use it in GitHub Desktop.
Save bernerdschaefer/487567 to your computer and use it in GitHub Desktop.
require 'mongoid'
require 'rspec'
Mongoid.configure do |config|
config.master = Mongo::Connection.new.db('testing')
end
class Person
include Mongoid::Document
collection.remove
field :ssn
embeds_one :pet
embeds_many :favorites
accepts_nested_attributes_for :pet, :favorites
end
class Pet
include Mongoid::Document
field :name
embedded_in :person, :inverse_of => :pet
end
class Favorite
include Mongoid::Document
field :title
embedded_in :person, :inverse_of => :favorites
end
describe Mongoid::Associations do
context "embeds_one" do
describe "creating associated document" do
let(:person) { Person.create!( :ssn => "1234", :pet_attributes => { :name => 'odie' } ) }
specify { person.reload.pet.name.should == 'odie' }
end
describe "updating associated document" do
let(:person) { Person.create!( :ssn => "1234", :pet_attributes => { :name => 'garfield' } ) }
before { person.update_attributes(:pet_attributes => { :name => 'odie' } ) }
specify { person.reload.pet.name.should == 'odie' }
end
end
context "embeds_many" do
describe "creating associated document" do
let(:person) { Person.create!( :ssn => "1234", :favorites_attributes => { '0' => { :title => 'something' } } ) }
specify { person.reload.favorites.first.title.should == 'something' }
end
describe "updating associated document" do
let(:person) { Person.create!( :ssn => "1234", :favorites => { '0' => { :title => 'nothing' } } ) }
before { person.update_attributes(:favorites_attributes => { '0' => { :title => 'something' } } ) }
specify { person.reload.favorites.first.title.should == 'something' }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment