[Rails] delete_if doesn't work for has_and_belongs_to_many

John Anderson ardour at semiosix.com
Tue Aug 8 20:54:11 GMT 2006


On Tue, 2006-08-08 at 14:22 -0400, dblack at wobblini.net wrote:
> On Tue, 8 Aug 2006, John Anderson wrote:
> 
> > On Wed, 2006-08-09 at 02:47 +1200, Jonathan Viney 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?
> 
> 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




More information about the Rails mailing list