aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlban Peignier2015-05-22 14:43:40 +0200
committerAlban Peignier2015-05-22 14:43:40 +0200
commitbc5a673bfa14b57cbe8cd971b0a4665e74f7fc59 (patch)
tree5d698246504ca8b8cd22ac98f0f61e0dd815c879
parent7f76897173cc3050bb9b4c5a34f47f684b2a1ade (diff)
downloadchouette-core-bc5a673bfa14b57cbe8cd971b0a4665e74f7fc59.tar.bz2
Refactor SetOrganisationIdToRps to update_all models (instead of loading them)
-rw-r--r--db/migrate/20150218175300_set_organisation_id_to_rps.rb18
1 files changed, 6 insertions, 12 deletions
diff --git a/db/migrate/20150218175300_set_organisation_id_to_rps.rb b/db/migrate/20150218175300_set_organisation_id_to_rps.rb
index 8572a5894..024f5938e 100644
--- a/db/migrate/20150218175300_set_organisation_id_to_rps.rb
+++ b/db/migrate/20150218175300_set_organisation_id_to_rps.rb
@@ -1,19 +1,13 @@
class SetOrganisationIdToRps < ActiveRecord::Migration
- class RuleParameterSet < ActiveRecord::Base
- attr_accessor :referential_id
- attr_accessor :organisation_id
- attr_accessor :name
- end
def up
- RuleParameterSet.all.each_with_index do |rps, index|
- rps.update_attributes :organisation_id => Referential.find( rps.referential_id).organisation_id,
- :name => "{rps.name} {index}"
- end
+ RuleParameterSet.where(organisation_id: nil).update_all "name = concat(name, ' ', id)"
+
+ # RuleParameterSet.joins(...).update_all("organisation_id = referentials.organisation_id")
+ # fails (see https://github.com/rails/arel/pull/294)
+ execute "UPDATE rule_parameter_sets SET organisation_id = referentials.organisation_id FROM referentials WHERE referentials.id = rule_parameter_sets.referential_id;"
end
def down
- RuleParameterSet.all.each_with_index do |rps, index|
- rps.update_attributes :organisation_id => nil
- end
+ RuleParameterSet.update_all organisation_id: nil
end
end