Forum: Ruby on Rails ActiveRecord delete_all With Sanitized Parameters?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D046cca1a33655b6285065ec89711389?d=identicon&s=25 Lance Ball (Guest)
on 2006-01-04 20:03
(Received via mailing list)
Hi there

I'm trying to delete a set of active record objects based on certain
conditions.  I'd like to do something like the following:

      Context.delete_all("uri IN (?)", uris)

But delete_all doesn't allow multiple arguments.  Since I don't have
the IDs for the objects I want to delete, I can't use delete(id), and
have resorted to this, which is significantly slower since it has to
instantiate each object, and there are individual SQL statements for
each delete.  Am I missing something obvious?

       contexts = Context.find(:all, :include => :terms,
         :conditions => ["uri IN (?)", uris])
       contexts.each do |context|
         context.destroy
       end

Thanks
Lance
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-01-04 20:23
Lance Ball wrote:

>       Context.delete_all("uri IN (?)", uris)

did you try 'Context.delete_all(["uri IN (?)",uris])' ?
D046cca1a33655b6285065ec89711389?d=identicon&s=25 Lance Ball (Guest)
on 2006-01-04 22:03
(Received via mailing list)
On 1/4/06, Kevin Olbrich <kevin.olbrich@duke.edu> wrote:
> Lance Ball wrote:
>
> >       Context.delete_all("uri IN (?)", uris)
>
> did you try 'Context.delete_all(["uri IN (?)",uris])' ?

Well, I guess we all have our moments to be _very_embarrassed_!

Thanks - that did the trick.

Lance
D046cca1a33655b6285065ec89711389?d=identicon&s=25 Lance Ball (Guest)
on 2006-01-04 22:07
(Received via mailing list)
Bad form to reply to my own post, but...

This does work, however, it has a drawback.  Using context.destroy
also removes associated rows in my habtm mapping table.
Context.delete_all does not.  Back to the drawing board...

Lance
This topic is locked and can not be replied to.