# Delete\_if doesn't work for has\_and\_belongs\_to\_many

**URL:** <https://www.ruby-forum.com/t/delete-if-doesnt-work-for-has-and-belongs-to-many/101878>\
**Category:** Rails\
**Created:** [August 8, 2006, 2:26pm UTC](https://www.ruby-forum.com/t/delete-if-doesnt-work-for-has-and-belongs-to-many/101878 "2006-08-08T14:26:35Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![john-anes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/22d042/32.png) [@john-anes](https://www.ruby-forum.com/u/john-anes)\
**Post date:** [August 8, 2006, 2:26pm UTC](https://www.ruby-forum.com/t/delete-if-doesnt-work-for-has-and-belongs-to-many/101878/1 "2006-08-08T14:26:35Z")

</div>

I’m using Rails-1.1.4 with Ruby-1.8.4

So let’s say I have 2 model classes:

class Item  
has\_and\_belongs\_to\_many :things  
end

class Thing  
has\_and\_belongs\_to\_many :items  
end

i = Item.new  
i.things \<\< Thing.new( :value =\> “Something”).save  
i.things \<\< Thing.new( :value =\> “Something Else” ).save

i.save

Fine, now I have two records in the items\_things table. Now if I say

t = Thing.find\_by\_value ‘Something’  
i.things.delete t  
i.save

Then I have only one record in the items\_things table. Which is correct.  
But, if I had said

i.things.delete\_if {|x| x.value = ‘Something’}  
i.save

then the relevant record is not deleted from the items\_things table.

Is this a bug? If so, where’s the best place to report it?

bye  
John

---

<div class="post-metadata">

**Author:** ![john-anes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/22d042/32.png) [@john-anes](https://www.ruby-forum.com/u/john-anes)\
**Post date:** [August 8, 2006, 2:41pm UTC](https://www.ruby-forum.com/t/delete-if-doesnt-work-for-has-and-belongs-to-many/101878/2 "2006-08-08T14:41:44Z")

</div>

Hi –

On Tue, 8 Aug 2006, John A. wrote:

> end  
> i.things.delete t  
> Is this a bug? If so, where’s the best place to report it?  
> There’s no delete\_if method defined for ActiveRecord collections,  
> other than the regular Array#delete\_if, so it’s not a bug. It could  
> be a candidate for a patch, I imagine.

David

–  
[http://www.rubypowerandlight.com](http://www.rubypowerandlight.com) =\> Ruby/Rails training & consultancy  
----\> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! \<-----  
[http://dablog.rubypal.com](http://dablog.rubypal.com) =\> D[avid]A[.]B[lack’s][Web]log  
[Ruby for Rails](http://www.manning.com/black) =\> book, Ruby for Rails  
[http://www.rubycentral.org](http://www.rubycentral.org) =\> Ruby Central, Inc.

---

<div class="post-metadata">

**Author:** ![john-anes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/22d042/32.png) [@john-anes](https://www.ruby-forum.com/u/john-anes)\
**Post date:** [August 8, 2006, 3:14pm UTC](https://www.ruby-forum.com/t/delete-if-doesnt-work-for-has-and-belongs-to-many/101878/3 "2006-08-08T15:14:47Z")

</div>

On Tue, 2006-08-08 at 08:39 -0400, removed\_email\_address@domain.invalid wrote:

> There’s no delete\_if method defined for ActiveRecord collections,  
> other than the regular Array#delete\_if, so it’s not a bug. It could  
> be a candidate for a patch, I imagine.

Ah, OK. What was the rationale behind implementing delete but not  
delete\_if?

bye  
John

---

<div class="post-metadata">

**Author:** ![john-anes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/22d042/32.png) [@john-anes](https://www.ruby-forum.com/u/john-anes)\
**Post date:** [August 8, 2006, 3:20pm UTC](https://www.ruby-forum.com/t/delete-if-doesnt-work-for-has-and-belongs-to-many/101878/4 "2006-08-08T15:20:48Z")

</div>

Hi –

On Tue, 8 Aug 2006, John A. wrote:

> > There’s no delete\_if method defined for ActiveRecord collections,  
> > other than the regular Array#delete\_if, so it’s not a bug. It could  
> > be a candidate for a patch, I imagine.
> 
> Ah, OK. What was the rationale behind implementing delete but not  
> delete\_if?

delete\_all and/or destroy\_all, with conditions, should serve pretty  
much the same purpose.

David

–  
[http://www.rubypowerandlight.com](http://www.rubypowerandlight.com) =\> Ruby/Rails training & consultancy  
----\> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! \<-----  
[http://dablog.rubypal.com](http://dablog.rubypal.com) =\> D[avid]A[.]B[lack’s][Web]log  
[Ruby for Rails](http://www.manning.com/black) =\> book, Ruby for Rails  
[http://www.rubycentral.org](http://www.rubycentral.org) =\> Ruby Central, Inc.

---

<div class="post-metadata">

**Author:** ![john-anes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/22d042/32.png) [@john-anes](https://www.ruby-forum.com/u/john-anes)\
**Post date:** [August 8, 2006, 4:48pm UTC](https://www.ruby-forum.com/t/delete-if-doesnt-work-for-has-and-belongs-to-many/101878/5 "2006-08-08T16:48:01Z")

</div>

That would be association\_collection.rb

But I’m not sure you want methods like delete\_if and friends to  
actually delete the objects from the database. You still need to be  
able to manipulate an array of records as just that, an array. There  
has to be a balance.

-Jonathan.

---

<div class="post-metadata">

**Author:** ![john-anes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/22d042/32.png) [@john-anes](https://www.ruby-forum.com/u/john-anes)\
**Post date:** [August 8, 2006, 4:17pm UTC](https://www.ruby-forum.com/t/delete-if-doesnt-work-for-has-and-belongs-to-many/101878/6 "2006-08-08T16:17:58Z")

</div>

On Tue, 2006-08-08 at 09:20 -0400, removed\_email\_address@domain.invalid wrote:

> > > > Is this a bug? If so, where’s the best place to report it?
> > > 
> > > There’s no delete\_if method defined for ActiveRecord collections,  
> > > other than the regular Array#delete\_if, so it’s not a bug. It could  
> > > be a candidate for a patch, I imagine.
> > 
> > Ah, OK. What was the rationale behind implementing delete but not  
> > delete\_if?
> 
> delete\_all and/or destroy\_all, with conditions, should serve pretty  
> much the same purpose.

It’s the ORM mismatch again - delete\_if works with objects whereas  
delete\_all works with records. Looks like it would be fairly simple to  
implement delete\_if in terms of delete\_all though? Any idea what file I  
could start looking at?

bye  
John

---

<div class="post-metadata">

**Author:** ![john-anes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/22d042/32.png) [@john-anes](https://www.ruby-forum.com/u/john-anes)\
**Post date:** [August 8, 2006, 6:21pm UTC](https://www.ruby-forum.com/t/delete-if-doesnt-work-for-has-and-belongs-to-many/101878/7 "2006-08-08T18:21:15Z")

</div>

On Wed, 2006-08-09 at 02:47 +1200, Jonathan V. wrote:

> That would be association\_collection.rb
> 
> But I’m not sure you want methods like delete\_if and friends to  
> actually delete the objects from the database. You still need to be  
> able to manipulate an array of records as just that, an array. There  
> has to be a balance.

OK, let me play Devil’s Advocate then. What would the use case be where  
one wants to delete an element from the array without deleting the  
corresponding record from the db?

bye  
John

---

<div class="post-metadata">

**Author:** ![john-anes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/22d042/32.png) [@john-anes](https://www.ruby-forum.com/u/john-anes)\
**Post date:** [August 8, 2006, 8:57pm UTC](https://www.ruby-forum.com/t/delete-if-doesnt-work-for-has-and-belongs-to-many/101878/8 "2006-08-08T20:57:28Z")

</div>

Hi –

On Tue, 8 Aug 2006, John A. wrote:

> corresponding record from the db?  
> Back at ya’:

Given that there are various operations that can be performed,  
including just manipulating the array/collection in memory, why should  
AR have to choose one operation from those available, and make as many  
constructs as possible perform it? Why does there have to be a  
“winner”, when the situation is already such that there’s at least one  
way to do all of the things in question?

David

–  
[http://www.rubypowerandlight.com](http://www.rubypowerandlight.com) =\> Ruby/Rails training & consultancy  
----\> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! \<-----  
[http://dablog.rubypal.com](http://dablog.rubypal.com) =\> D[avid]A[.]B[lack’s][Web]log  
[Ruby for Rails](http://www.manning.com/black) =\> book, Ruby for Rails  
[http://www.rubycentral.org](http://www.rubycentral.org) =\> Ruby Central, Inc.

---

<div class="post-metadata">

**Author:** ![john-anes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/22d042/32.png) [@john-anes](https://www.ruby-forum.com/u/john-anes)\
**Post date:** [August 9, 2006, 3:18am UTC](https://www.ruby-forum.com/t/delete-if-doesnt-work-for-has-and-belongs-to-many/101878/9 "2006-08-09T03:18:50Z")

</div>

On Tue, 2006-08-08 at 14:22 -0400, removed\_email\_address@domain.invalid wrote:

> > OK, let me play Devil’s Advocate then. What would the use case be where  
> > one wants to delete an element from the array without deleting the  
> > corresponding record from the db?
> 
> Back at ya’:
> 
> Given that there are various operations that can be performed,  
> including just manipulating the array/collection in memory,

Which is what Array is for, right? ActiveRecord exists precisely because  
of a need to manipulate objects (well, records ultimately) in db tables.

> why should  
> AR have to choose one operation from those available, and make as many  
> constructs as possible perform it?

Well, I figured that based on the behaviour with delete, I could safely  
assume that delete\_if (since it didn’t throw a missing method exception)  
would do the same kind of thing. I was surprised when they behaved  
differently.

> Why does there have to be a “winner”,

I’m not sure what you mean here?

> when the situation is already such that there’s at least one  
> way to do all of the things in question?

> From my point of view, another way of seeing it is this: why is there a  
> method that does something very unexpected (ie not manipulate records in  
> the db when that’s the express purpose of the collection in question),  
> when it doesn’t even need to exist since there are other ways to achieve  
> the same functionality?

And if one was in the mood for making tangential, fine and probably  
mostly irrelevant 😉 distinctions, one could say that delete\_if allows  
one to manipulate the collection from an object point of view, whereas  
delete\_all allows one to manipulate the collection from an SQL point of  
view.

bye  
John

---

<div class="post-metadata">

**Author:** ![john-anes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/22d042/32.png) [@john-anes](https://www.ruby-forum.com/u/john-anes)\
**Post date:** [August 8, 2006, 9:44pm UTC](https://www.ruby-forum.com/t/delete-if-doesnt-work-for-has-and-belongs-to-many/101878/10 "2006-08-08T21:44:49Z")

</div>

> Given that there are various operations that can be performed,  
> including just manipulating the array/collection in memory, why should  
> AR have to choose one operation from those available, and make as many  
> constructs as possible perform it? Why does there have to be a  
> “winner”, when the situation is already such that there’s at least one  
> way to do all of the things in question?

I think what David is saying (because it’s a bit cryptic) is that you  
already have the functionality you need via model#delete(:conditions =\>  
[]) without having to step on the toes of other, original array methods.

Given this, why bother with forcing ActiveRecord to chew up more methods  
than is needed? (My co-worker does this kind of stuff and it irks me to  
no end. It’s called #update\_attributes you fool! Sorry. 🙂

- Daniel

---

<div class="post-metadata">

**Author:** ![john-anes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/22d042/32.png) [@john-anes](https://www.ruby-forum.com/u/john-anes)\
**Post date:** [August 9, 2006, 10:37am UTC](https://www.ruby-forum.com/t/delete-if-doesnt-work-for-has-and-belongs-to-many/101878/11 "2006-08-09T10:37:26Z")

</div>

On Tue, 2006-08-08 at 21:44 +0200, Daniel W. wrote:

> > Given that there are various operations that can be performed,  
> > including just manipulating the array/collection in memory, why should  
> > AR have to choose one operation from those available, and make as many  
> > constructs as possible perform it? Why does there have to be a  
> > “winner”, when the situation is already such that there’s at least one  
> > way to do all of the things in question?
> 
> I think what David is saying (because it’s a bit cryptic) is that you  
> already have the functionality you need via model#delete(:conditions =\>  
> [])

Not at all. That was clear to me.

> without having to step on the toes of other, original array methods.

That’s exactly what I find puzzling. For me it’s not stepping on the  
toes of other methods, it’s making sure that if you’re masquerading as  
someone else then you do a proper job.

For Array, delete\_if is a more general form of delete - I can implement  
the delete functionality in terms of delete\_if (Well, except for the  
returning of the block if the item isn’t found). But I can’t do that  
with an AR collection. That strikes me as very odd.

> Given this, why bother with forcing ActiveRecord to chew up more methods  
> than is needed?

Because if you don’t do a good job masquerading as someone else, someone  
will see through your disguise.

In this case AssociationProxy implements method\_missing and forwards  
delete\_if to its underlying Array.

> (My co-worker does this kind of stuff and it irks me to  
> no end. It’s called #update\_attributes you fool! Sorry. 🙂

What kind of stuff? And what irks you, exactly?

bye  
John

---

<div class="post-metadata">

**Author:** ![john-anes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/22d042/32.png) [@john-anes](https://www.ruby-forum.com/u/john-anes)\
**Post date:** [August 15, 2006, 2:19pm UTC](https://www.ruby-forum.com/t/delete-if-doesnt-work-for-has-and-belongs-to-many/101878/12 "2006-08-15T14:19:22Z")

</div>

Hi –

On Tue, 8 Aug 2006, John A. wrote:

> > From my point of view, another way of seeing it is this: why is there a  
> > method that does something very unexpected (ie not manipulate records in  
> > the db when that’s the express purpose of the collection in question),  
> > when it doesn’t even need to exist since there are other ways to achieve  
> > the same functionality?

It’s interesting that push is magic, but pop isn’t 🙂 And same with  
delete and delete\_if. Also, while you can’t do:

ar\_collection \<\< 100

you _can_ do:

ar\_collection[1] = 100

I guess it’s a tradeoff between the vanilla array behavior and the  
special AR behavior – though in some of these cases a case could  
certainly be made for some kind of unification.

David

–  
[http://www.rubypowerandlight.com](http://www.rubypowerandlight.com) =\> Ruby/Rails training & consultancy  
----\> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! \<-----  
[http://dablog.rubypal.com](http://dablog.rubypal.com) =\> D[avid]A[.]B[lack’s][Web]log  
[Ruby for Rails](http://www.manning.com/black) =\> book, Ruby for Rails  
[http://www.rubycentral.org](http://www.rubycentral.org) =\> Ruby Central, Inc.
