# HABTM creating duplicate records in test environment

**URL:** <https://www.ruby-forum.com/t/habtm-creating-duplicate-records-in-test-environment/160779>\
**Category:** Rails\
**Created:** [February 19, 2009, 4:58pm UTC](https://www.ruby-forum.com/t/habtm-creating-duplicate-records-in-test-environment/160779 "2009-02-19T16:58:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@lee](https://www.ruby-forum.com/u/lee)\
**Post date:** [February 19, 2009, 4:58pm UTC](https://www.ruby-forum.com/t/habtm-creating-duplicate-records-in-test-environment/160779/1 "2009-02-19T16:58:02Z")

</div>

I have the following HABTM associations between two models,  
MemberEntityAccessibility and MemberEntity:

```
has_and_belongs_to_many :member_entities,
                          :class_name => "MemberEntity",
                          :join_table =>

```

“accessibilities\_member\_entities”,  
:foreign\_key =\> “accessibility\_id”,  
:association\_foreign\_key =\>  
“member\_entity\_id”

```
has_and_belongs_to_many :member_entity_accessibilities,
                        :class_name => "Accessibility",
                        :join_table =>

```

“accessibilities\_member\_entities”,  
:foreign\_key =\> “member\_entity\_id”,  
:association\_foreign\_key =\>  
“accessibility\_id”

Also in the MemberEntityAccessibility model I have the following code  
to allow a member (entity) to be added into the HABTM join table.

```
def add_member_entity?(entity)
    return false if entity.nil? || member_entities.include?

```

(entity)  
member\_entities \<\< entity  
true  
end

When I am testing my code in the test environment (fixtures etc), my  
exhaustive(!) logging reveals that ActiveRecord attempts to insert the  
same ‘entity’ record twice into the join table when there is only one  
member (entity) object in the collection (member\_entities). For  
example, where an entity object (id=1) is added to ‘member\_entities’  
of accessibility object (id=1), ActiveRecord saves two records, each  
with a compound key of 1,1.

My logging confirms that the member\_entities collection only has one  
entry (as expected) and not two.

Any ideas would be much appreciated - I have hit a wall with this one  
and wonder if it is something to do with the test environment.

---

<div class="post-metadata">

**Author:** ![lee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@lee](https://www.ruby-forum.com/u/lee)\
**Post date:** [February 20, 2009, 8:44am UTC](https://www.ruby-forum.com/t/habtm-creating-duplicate-records-in-test-environment/160779/2 "2009-02-20T08:44:08Z")

</div>

The same code runs fine in the development environment - that is, only  
one record is created in the join table.

Am I overlooking a subtlety in the use of the test environment? For  
example, I have not defined any fixtures for the join table.

---

<div class="post-metadata">

**Author:** ![lee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@lee](https://www.ruby-forum.com/u/lee)\
**Post date:** [February 20, 2009, 8:58am UTC](https://www.ruby-forum.com/t/habtm-creating-duplicate-records-in-test-environment/160779/3 "2009-02-20T08:58:13Z")

</div>

I should add that my test file looks like this:

require ‘test\_helper’

class MemberEntityTest \< ActiveSupport::TestCase

fixtures :users, :member\_entities, :properties

def setup

```
@administrator = member_entities(:administrator)
@bob = member_entities(:bob)

```

end

test “create a group” do

```
Group.create({:owner => @bob, :pingee_name => "London Albion

```

Players"})

```
#does Bob own the new group?
assert_equal @bob, new_group.owner

```

end  
end

The ‘create’ method in the Group model (which inherits from  
MemberEntity) create the join table records as well as a bunch of  
other stuff.

Thank you.
