ruby on rails 4 - ActiveRecord Query to identify orphan child records -
i have post , user models. every post belongs user. however, during database import, erroneous user_ids have been entered on posts. query posts user_ids don't refer user? thanks.
i following, result in 1 select
, 1 delete
statement
post.where('`posts`.`user_id` not in (?)', user.pluck(:id)).delete_all
Comments
Post a Comment