Sunday, May 1, 2011

usage of find_by_sql to delete record

I'm trying to delete few records from table 'abc' where id is fetched from 'xyz' table

SQL:

delete from abc where id in (select ids from xyz where i= 10)

this query i want to execute using find_by_sql to avoid multiple hits to database.

It's giving error. Please help me in getting this delete action done in a single hit to DB.

From stackoverflow
  • I don't think find_by_sql is the method you want.

    If you have

    class Abc < ActiveRecord::Base
    end
    

    (so abc.rb is your model) then I think you should be able to do something like this:

    Abc.connection.execute('delete from abc where id in (select ids from xyz where i= 10)')
    

0 comments:

Post a Comment