From bc5a673bfa14b57cbe8cd971b0a4665e74f7fc59 Mon Sep 17 00:00:00 2001 From: Alban Peignier Date: Fri, 22 May 2015 14:43:40 +0200 Subject: Refactor SetOrganisationIdToRps to update_all models (instead of loading them) --- .../20150218175300_set_organisation_id_to_rps.rb | 18 ++++++------------ 1 file 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 -- cgit v1.2.3