I have two models: TimeLog and Task. TimeLog belongs to Task and Task has many TimeLogs.
In the past some Tasks were deleted but the corresponding TimeLogs were not deleted (the cascade delete wasn’t working). So we have some broken TimeLogs. They do have a task_id but that task_id does not exist anymore.
I have two questions:
1) I want to get all the TimeLogs from a user but filtering the broken ones.
i.e
TimeLog.find(:all, :conditions => ['time_log.user_id = ? and <time_log.task_id exists>])
2) I want to get all the broken TimeLogs in the console to delete them manually.
i.e.
TimeLog.find(:all, :conditions => [<!time_log.task_id exists>])
How can I do that?
Thanks
Add something like the following
where()to your chain ofActiveRelationcalls to include any orphanedTimeLogs:…and obviously if you remove the
NOTyou’ll explicitly exclude any orphaned records.