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.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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. On 19 Feb, 15:57, Lee <Lee.Longm...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> I have the followingHABTMassociations 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 theHABTMjoin 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 tworecords, 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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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.
On 20 Feb, 07:43, Lee
<Lee.Longm...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
wrote:> 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.
>
> On 19 Feb, 15:57, Lee
<Lee.Longm...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:
>
> > I have the followingHABTMassociations 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 theHABTMjoin 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 tworecords, 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.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---