Forum: Ruby on Rails HABTM creating duplicate records in test environment

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
753038ae16700c0fd9a5e1d2e80f68f6?d=identicon&s=25 Lee (Guest)
on 2009-02-19 16:58
(Received via mailing list)
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.
753038ae16700c0fd9a5e1d2e80f68f6?d=identicon&s=25 Lee (Guest)
on 2009-02-20 08:44
(Received via mailing list)
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.
753038ae16700c0fd9a5e1d2e80f68f6?d=identicon&s=25 Lee (Guest)
on 2009-02-20 08:58
(Received via mailing list)
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.
This topic is locked and can not be replied to.