# Delete\_if does not work on associations

**URL:** <https://www.ruby-forum.com/t/delete-if-does-not-work-on-associations/159520>\
**Category:** Rails\
**Created:** [February 5, 2009, 10:33am UTC](https://www.ruby-forum.com/t/delete-if-does-not-work-on-associations/159520 "2009-02-05T10:33:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 5, 2009, 10:33am UTC](https://www.ruby-forum.com/t/delete-if-does-not-work-on-associations/159520/1 "2009-02-05T10:33:10Z")

</div>

I tried to user delete\_if on an association:

class Group \< ActiveRecord::Base  
has\_many :memberships, :dependent =\> :destroy  
has\_many :users, :through =\> :memberships

…  
end

This is the controller method, trying to use delete\_if. Although some  
elements are removed (s2 \< s1), the save method does not update the  
association.

def intersect\_or\_remove\_group  
other\_group = Group.find(params[:group\_id])  
s1 = @group.users.size  
case params[:submit]  
when “Intersect”  
@group.users.delete\_if { |u| !other\_group.users.include?(u) }  
when “Remove”  
@group.users.delete\_if { |u| other\_group.users.include?(u) }  
end  
s2 = @group.users.size  
if s2 \< s1  
@group.save  
end  
redirect\_to @group  
end

If I rewrite it like this, it works just fine:

def intersect\_or\_remove\_group  
other\_group = Group.find(params[:group\_id])  
s1 = @group.users.size  
to\_be\_deleted = Array.new  
case params[:submit]  
when “Intersect”  
@group.users.each { |u| to\_be\_deleted \<\< u if !  
other\_group.users.include?(u) }  
when “Remove”  
@group.users.each { |u| to\_be\_deleted \<\< u if  
other\_group.users.include?(u) }  
end  
to\_be\_deleted.each { |u| @group.users.delete(u) }  
s2 = @group.users.size  
if s2 \< s1  
@group.save  
end  
redirect\_to @group  
end

Is this a bug or a feature?

Thanks for any help in advance

Regards

Alexander

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 5, 2009, 10:39am UTC](https://www.ruby-forum.com/t/delete-if-does-not-work-on-associations/159520/2 "2009-02-05T10:39:36Z")

</div>

On Feb 4, 11:33 pm, “removed\_email\_address@domain.invalid”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> elements are removed (s2 \< s1), the save method does not update the  
> association.

Looks like delete\_if just acts on the target array, not the  
association itself (as a slight typing saver you can pass an array to  
delete.

Fred

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 5, 2009, 7:57pm UTC](https://www.ruby-forum.com/t/delete-if-does-not-work-on-associations/159520/3 "2009-02-05T19:57:23Z")

</div>

Thanks for the hint.

Probably this is a bug, because the association is not updated  
correctly. Unfortunately I am not advanced enough in rails to be able  
to provide a patch for it.

Alexander alias MrLongleg
