I think this would be a bit faster (assuming fact names are unique).

replace line 85 with: fact_names = FactName.maximum(:id, :group => 'name')
change line 94 to: 
fact_values.build(:value => v, :fact_name => fact_names.keys.include?(name) ?  fact_names[name] : FactName.create!(:name => name))