# Deleting associations in variable

**URL:** <https://www.ruby-forum.com/t/deleting-associations-in-variable/123775>\
**Category:** Rails\
**Created:** [December 20, 2007, 2:46pm UTC](https://www.ruby-forum.com/t/deleting-associations-in-variable/123775 "2007-12-20T14:46:38Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [December 20, 2007, 2:46pm UTC](https://www.ruby-forum.com/t/deleting-associations-in-variable/123775/1 "2007-12-20T14:46:38Z")

</div>

Hi all,

Since I cannot get my records in 1 query I query more data than wanted  
and delete the rendundand rows afterwards. I’m trying to remove an  
association after I queried the database. (This association changes  
shouldn’t be comitted to the database)

First I query the database for events and its attendees. Of some  
events I want to remove the attendees. I do this via 2 foreach loops  
(all events and all attendees in event).

I try to set the attendees to empty by event.attendees = Array.new but  
sometimes I get an error: RangeError: is recycled object . Also this  
seems to delete my records in the database!

Maybe someone has a better apporoach?

@kinds = Kind.find(:all, :select =\> “distinct id,  
value”, :conditions =\> {:kind =\> ‘event’} )  
events\_temp = Event.find(:all, :select =\> :distinct, :include  
=\> :attendees, :conditions =\> [ “events.node\_id = ?”,  
session[:club\_id]], :order =\> “start DESC” )  
@events = Array.new  
counter = 11  
for event in events\_temp do  
if event.attendees.empty?  
@events.push(event)  
elsif  
event.attendees.each\_with\_index do |attendee, index|  
if (attendee.user\_id == session[:user\_id])  
@events.push(event)  
elsif (event.attendees.count == index + 1)  
event.attendees = Array.new  
@events.push(event)  
end  
end  
end  
end
