How would I recreate the following query in Rails speak so that it returns the amount of rows processed?
self.connection.execute('DELETE FROM `cd_artist`
LEFT JOIN `cdpedia` ON cd_artist.id = cdpedia.`artistId`
WHERE artistId IS NULL;')
I realize I should use something like Artist.find_by_sql for it to return something, but that gives me an error.
Perhaps there is a more elegant way of doing this?
I want to avoid the Rails way of first searching the database, then grabbing the IDs and deleting the entries with Object.destroy or even Object.delete(id) since a SQL call is so much faster (if you have the right indexes), but I’m curious as to what the “proper” way to do it is.
You just have to replace execute by delete, and it will return the number of rows affected.
There is maybe a more elegant way to do this with ActiveRecord, but I doubt it will be as efficient as a raw SQL query.