I have a specific use case where some of my model tables are in fact views. I use views to aggregate data from different sources in a single model table. More specifically, I have local rows and remote rows fetched from a remote database that I want read-only. local rows are writable. For the rows that are writable, I’d like to make use of Active Record to insert, update or delete those rows.
Because the underlying table is a non trivial view, the view itself is non updatable, so I’d like to tell Active Record to use a different table name when performing an INSERT, UPDATE or DELETE operation.
Looking at the code it seems relatively simple to hook in the right place. I started writing an implementation here: activerecord: Implement different table names for different operations by mildred · Pull Request #42712 · rails/rails · GitHub. I haven’t fixed all tests yet (I need to set up a dev environment first).
I’d like to gather feedback about this feature (I started opening an issue but was redirected here). Is it going to be accepted as a feature? If not, can the code around this feature be organized in a way that it’s possible to override a method to manually implement it. Specifically, I added an argument to the arel_table method to allow implementing this method and return a different result depending of the operation type.