Nine tips for Rails migration mastery

25-Oct-2018 1946
Several guidelines to help you navigate the many database changes you'll make throughout your career as a Rails developer.When backfilling or transforming data in a migration, rather than referencing any Active Record objects, opt to write them in raw SQL instead. If for no other reason, it's good for the brain to remember how to do it without the convenience of an ORM.But the real reasons are that raw SQL executes much faster, leading to reduced downtime or error rates on deploy, and that references to app code that is changed/moved will cause surprises and confusion. Because migrations run in many different environments at many different times, the odds are that if someone changes a constant name, for example, that could cause an incoming migration that references it to break.If you absolutely must use Active Record, define whatever model you need within the migration. That way, it remains separate from your app code but you still have the ability to work with data in Ruby. In my experience this very rarely happens.
Use coupon code:

RUBYONRAILS

to get 30% discount on our bundle!
Prepare for your next tech interview with our comprehensive collection of programming interview guides. Covering JavaScript, Ruby on Rails, React, and Python, these highly-rated books offer thousands of essential questions and answers to boost your interview success. Buy our 'Ultimate Job Interview Preparation eBook Bundle' featuring 2200+ questions across multiple languages. Ultimate Job Interview Preparation eBook Bundle