# Polymorphic join, object.destroy issue

**URL:** <https://www.ruby-forum.com/t/polymorphic-join-object-destroy-issue/152223>\
**Category:** Rails\
**Created:** [October 31, 2008, 3:13pm UTC](https://www.ruby-forum.com/t/polymorphic-join-object-destroy-issue/152223 "2008-10-31T15:13:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![railsdog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b77776/32.png) [@railsdog](https://www.ruby-forum.com/u/railsdog)\
**Post date:** [October 31, 2008, 3:13pm UTC](https://www.ruby-forum.com/t/polymorphic-join-object-destroy-issue/152223/1 "2008-10-31T15:13:51Z")

</div>

My generic routine looks like this:

def update\_linked\_poly\_side(base\_side, ids, poly\_side)

# get back the current linked items, and the base part

# of the polymorphic field names, like ‘appllinkable’

linked, poly\_base = self.polymorphic\_info base\_side  
id\_attr = base\_side+’\_id’  
linked.each do |link|  
linked\_model\_id = (link.send id\_attr).to\_s  
link.destroy unless ids.include(linked\_model\_id)  
ids.delete(linked\_model\_id)  
end  
ids.each do |id|  
linked.create(blah blah blah - create works fine)  
end  
end

The link.destroy works fine for the first link that is currently  
linked but isn’t in the ids list, but it doesn’t work for any  
subsequent links that should also be destroyed.

Runtime logging (not in the code above for clarity) reports:  
at entry,  
3 appllink objects, ids [“3”, “1”, “2”] in linked (correct)  
poly\_base = ‘applinkable’ (correct)  
ids = ["", “2”]  
in the linked.each do |link| loop  
it looks for “3” in ids, doesn’t find it and logs:  
SQL (0.000694) BEGIN  
Appllink Destroy (0.000345) DELETE FROM ‘appllinks’  
WHERE ‘id’ = 9  
SQL (0.001159) COMMIT

then it looks for “1” in ids, doesn’t find it and logs:  
SQL (0.000138) BEGIN  
SQL (0.000137) COMMIT

There isn’t an issue with the first linked item that is to be deleted,  
but there isn’t a SQL statement generated for any subsequent items that  
should be removed… adding new linked items works just fine (that’s in  
the ids.each do |id| loop).

Any ideas? Anyone? Oh, and Rails 2.1.0, Ubuntu 8.04, Ruby 1.8.6

---

<div class="post-metadata">

**Author:** ![railsdog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b77776/32.png) [@railsdog](https://www.ruby-forum.com/u/railsdog)\
**Post date:** [October 31, 2008, 3:55pm UTC](https://www.ruby-forum.com/t/polymorphic-join-object-destroy-issue/152223/2 "2008-10-31T15:55:13Z")

</div>

Curiouser and curiouser…

I changed the config.cache\_store from :mem\_cache\_store to :file\_store,  
and the proper SQLs are generated for all the deletes.

Clearly I have some reading up on memcached to do… perhaps this was  
tied up in some model caching I wasn’t aware that I was doing.
