ModelsCollection messed up after recursive update has_many through

1 view
Skip to first unread message

Scott Krieger

unread,
Nov 10, 2009, 12:22:38 PM11/10/09
to restfulx-...@googlegroups.com
I have a tag user control that
model.yml:
tagging:
 - belongs_to: [tag,thing]
tag:
 - name: string
 - has_many_through: [taggings, thing]
thing:
 - name: string
 - has_many_through: [taggings, tags]
In ThingBox.mxml:

private function saveThing():void {

  updateModelProperties();

  if (thing.id) {

    thing.update({onSuccess: onThingUpdate,recursive:true});

  } else {

    thing.create({onSuccess: onThingCreate,recursive:true});

  }

}

private function updateModelProperties():void {

  thing.name = nameTextInput.text;

  thing.tags = setUpTagsForUpdate(tagsAutoComplete.selectedItems);

}

private function setUpTagsForUpdate(tags:ArrayCollection):ModelsCollection{

  var currentTags:ModelsCollection= new ModelsCollection;

  for(var i:int = 0;i<tags.length;i++){// walk through the tags

   // find the tag

   if(tags[i] is Tag){

     currentTags.addItem(tags[i]);

   }else{

     // if it doesn't exist, create it

     var myTag:Tag = new Tag();// create new tag

     myTag.name = tags[i];

     myTag.thing=thing;

     currentTags.addItem(myTag);

   }

 }

 return currentTags;

}

Thing Model:

class Thing < ActiveRecord::Base
before_save :blitz_taggings
has_many :taggings
has_many :tags, :through => :taggings
accepts_nested_attributes_for :tags
accepts_nested_attributes_for :taggings

def tags_attributes=(tags_attributes)
self.tags.clear
tags_attributes.uniq.each do |tag|
self.tags << Tag.find_or_create_by_name(tag)
end
end
def blitz_taggings
self.taggings.destroy
end

end

Everything is fine on the rails side, and everything works great as
long as I'm dealing with existing tags. But if I try to create a new
tag, my Tag ModelsCollection gets messed up. Here's what I see in
Flash Builder when I break at onThingUpdate:

Rx.models.index(Tag) = org.restfulx.collections.ModelsCollection (@fa74bc9)
[0] = tagger.models.Tag (@fe88629)
[inherited] =
_110331118thing = org.restfulx.collections.ModelsCollection (@fe7eca1)
_3373707name = "test4"
_756757922taggings = org.restfulx.collections.ModelsCollection (@fe7ef29)
name = "test4"
taggings = org.restfulx.collections.ModelsCollection (@fe7ef29)
thing = org.restfulx.collections.ModelsCollection (@fe7eca1)
autoUpdateCounter = 0
dispatchResetEvent = true
eventDispatcher = flash.events.EventDispatcher (@fa74b81)
filterFunction = <getter>
_filterFunction = Function (@fcdf791)
filterFunctions = null
_filters = null
length = 1
list = mx.collections.ArrayList (@fa7b921)
_list = mx.collections.ArrayList (@fa7b921)
localIndex = Array (@fe1a079)
metadata = Object (@fd17241)
pendingUpdates = null
resourceManager = mx.resources.ResourceManagerImpl (@f5e6299)
revision = 6
sort = null
_sort = null
source = Array (@fb99cb9)
[0] = tagger.models.Tag (@fd30a61)
[1] = tagger.models.Tag (@fd30101)
[2] = tagger.models.Tag (@fd301f1)
[3] = tagger.models.Tag (@fd303d1)
[4] = tagger.models.Tag (@fd305b1)
[5] = tagger.models.Tag (@fd30629)
[6] = tagger.models.Tag (@fe88b51)
[7] = tagger.models.Tag (@fe88629)
[8] = tagger.models.Tag (@fe88f89)
length = 9


So in this scenario, I have 7 existing tags and I just added one new
one. All previous tags now appear as blanks and only the new tag works
as expected. If I refresh, everything is fine. I hope this is enough
info for someone to show me what I'm doing wrong. Thanks alot.

Scott Krieger

Scott

unread,
Nov 10, 2009, 5:09:47 PM11/10/09
to restfulx-framework
Typo in my model.yml:

model.yml:
tagging:
- belongs_to: [tag,thing]
tag:
- name: string
- has_many_through: [taggings, things]
thing:
- name: string
- has_many_through: [taggings, tags]

But that does not resolve the issue. Fingers crossed this is a simple
one.

ahabman

unread,
Nov 23, 2009, 9:06:21 AM11/23/09
to restfulx-framework
ping
Reply all
Reply to author
Forward
0 new messages