// if you add a item in the middle of the list, it will go at the end when you fetch it
// due to the ordering by ID when calling asList()
RelatedManyParent god = new RelatedManyParent("god");
List<RelatedManyChild> adams = new ArrayList<RelatedManyChild>();
for(int i=0; i<100; i++){
RelatedManyChild adam = new RelatedManyChild("adam"+i);
god.children.asList().add(adam);
adams.add(adam);
}
god.insert();
assertNotNull(god.id);
for(int i=0; i<100; i++){
assertNotNull(adams.get(i).id);
assertEquals(god.id, adams.get(i).owner.id);
}
// add
RelatedManyChild adam_chboing = new RelatedManyChild("adam_chboing");
god.children.asList().add(57, adam_chboing);
god.update();
RelatedManyParent godbis = Model.all(RelatedManyParent.class).filter("name", god.name).get();
List<RelatedManyChild> children = godbis.children.asList();
for(int i=0; i<101; i++){
if(i<100){
assertEquals(adams.get(i), children.get(i));
}else {
assertEquals(adam_chboing, children.get(i));
}
}
RelatedManyChild adam_chboing_bis = RelatedManyChild.all().filter("id", adam_chboing.id).get();
assertEquals(god.id, adam_chboing_bis.owner.id);
}