Skip to content
Browse files

Fixing multiple assignment of has_one assocaition

  • Loading branch information...
1 parent 283a810 commit 54d7eb31d9a3eb97a6ead0b06ad9205713d06c09 @endeepak committed Dec 10, 2011
View
3 lib/neo4j/rails/mapping/property.rb
@@ -55,8 +55,7 @@ def #{rel_type}_rel
def #{rel_type}=(other)
dsl = _decl_rels_for(:'#{rel_type}')
storage = _create_or_get_storage_for_decl_rels(dsl)
- rel = storage.single_relationship(dsl.dir)
- rel && rel.destroy
+ storage.destroy_single_relationship(dsl.dir)
storage.create_relationship_to(other, dsl.dir)
end
RUBY
View
5 lib/neo4j/rails/relationships/storage.rb
@@ -122,6 +122,11 @@ def single_relationship(dir, raw = false)
end
end
+ def destroy_single_relationship(dir)
+ rel = single_relationship(dir)
+ rel && rel.destroy && relationships(dir).delete(rel)
+ end
+
def all_relationships(dir)
Enumerator.new(self, :each_rel, dir)
end
View
12 spec/rails/relationship_spec.rb
@@ -421,6 +421,18 @@ def to_s
jack.thing.should be_nil
end
+ it "has_one: should not create duplicate relationships on multiple assignment" do
+ clazz = create_model
+ clazz.has_one :thing
+
+ jack = clazz.create!
+ jack.thing = Neo4j::Model.create!
+ jack.thing = Neo4j::Model.create!
+ jack.save!
+
+ expect { jack.thing }.not_to raise_error
+ end
+
it "add nodes to a has_one method with the #new method" do
member = Member.new
avatar = Avatar.new

0 comments on commit 54d7eb3

Please sign in to comment.
Something went wrong with that request. Please try again.