Schema Manipulation Outside Migrations
All the migration methods described so far in this chapter are also available as methods on Active Record connection objects and so are accessible within the models, views, and controllers of a Rails application.
For example, you might have discovered that a particular long-running report runs a lot faster if the orders table has an index on the city column. But that index isn’t needed during the day-to-day running of the application, and tests have shown that maintaining it slows the application appreciably.
Let’s write a method that creates the index, runs a block of code, and then drops the index. This could be a private method in the model or could be implemented in a library:
| | def run_with_index ... |
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access