Guide me through refactoring raw SQL to ORM patterns in a Python learning management system project in a {{tone}} tone. Tech stack: Ruby on Rails. Developer context: tech lead. Provide a plan covering: assessment of current code smells and affected files, 2-3 refactoring strategies with effort and risk, safety measures including test coverage and rollback, step-by-step refactoring with before/after code snippets, advanced improvements like SOLID principles and performance, and verification steps. Include real, compilable Python code. Each step must leave the codebase in a working state. Maximum 1800 words.