indeed, but truncate would delete all record from the table whereas I
just want to selectively delete those rows that have a site_id column
matching the id value passed to the function.
class Site
has_many :site_products, :dependent => :destroy
has_many :site_purchases, :dependent => :destroy
has_many :site_stats, :dependent => :destroy
end
@site.destroy
Which may be just as slow...
Or, if you don't need to actually instantiate and destroy each record (for example, so callbacks will run), then you can replace the associations with :dependent => :delete_all. That should result in SQL like "DELETE FROM site_products WHERE site_id = #{params[:id]}"
try delete_all - http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M001382
which will delete everything in 1 SQL statement. If you need to worry
about dependent associations or callbacks try destroy_all which will
do the looping for you (but be just as slow).