SELECT *FROM "images"WHERE ( ( "images"."imageable_type" = 'articles' AND NOT EXISTS ( SELECT * FROM "articles" WHERE "images"."imageable_id" = "articles"."id" ) ) OR ( "images"."imageable_type" = 'user_profiles' AND NOT EXISTS ( SELECT * FROM "user_profiles" WHERE "images"."imageable_id" = "user_profiles"."id" ) ) )