這裏是場景導軌遷移版本兼容性
生產/分段代碼是關於版本的代碼爲X
版X
# order model
class Order < ActiveRecord::Base
has_many :payment_transactions
# has column for check_number
def update_report
ReportTable.where(:order_id => id).first.update_attributes(:check_number => check_number)
end
end
# payment_transaction model
class PaymentTransaction < ActiveRecord::Base
end
版X的代碼+ 5
# migration
Order.all.map{|x| x.update_report }
版本X + 10的代碼(當前)
# migration
add_column :payment_transactions, :check_number, :integer
# order model
class Order < ActiveRecord::Base
has_many :payment_transactions
# moved the column check_number to payment_transactions
def check_number
self.payment_transactions.where(:method => 'check').blank? ? nil : self.payment_transactions.where(:method => 'check').first.check_number
end
def update_report
ReportTable.where(:order_id => id).first.update_attributes(:check_number => check_number)
end
end
# payment_transaction model
class PaymentTransaction < ActiveRecord::Base
# has column for check_number
end
現在,當我在臨時環境到最新版本(X + 10),更新代碼並運行遷移,在X + 5的遷移,因爲它試圖運行此
def check_number
self.payment_transactions.where(:method => 'check').blank? ? nil : self.payment_transactions.where(:method => 'check').first.check_number
end
和payment_transaction會失敗在X + 10遷移之前不會獲取check_number字段。
最好的處理方法是什麼?
這種方法的問題是,我添加更新的簡單版本報告。那就更復雜了。 – Addy