I've changed it to:

```ruby
 FactValue.update_all(
            ["fact_name_id=?", fact_name_id],
            ["fact_name_id in (select t.id from (select * from fact_names) t where t.name=?) and fact_name_id<>?", fact_name, fact_name_id])
        UserFact.update_all(
            ["fact_name_id=?", fact_name_id],
            ["fact_name_id in (select t.id from (select * from fact_names) t where t.name=?) and fact_name_id<>?", fact_name, fact_name_id])
        FactName.delete_all(["id in (select t.id from (select * from fact_names) t where t.name=?) and id<>?", fact_name, fact_name_id])
      end
```
and it seems to work, but it took **1101.6213s** on my test system with 50k fact_names! (I'm sure there is a better SQL then mine here).