How to change ActiveRecord table name at run time

I change table_name_prefix during the launch of the rails application (it may seem strange, but really what I want). When table_name_prefix changes for ActiveRecord I reset the table names ( table_name and quoted_table_name ) by calling reset_table_name and they change .. however I have one more problem.

If the table name changes, after calling a thing such as counting or searching for an ActiveRecord object, it still works with the table that was used previously.

How to reach a child's ActiveRecord before reset, so that when the prefix, suffix, table_name changes it, does it work with the new settings?

Thank you for your help!

+6
activerecord ruby-on-rails-3
source share
1 answer

I found an explanation of the described behavior. Although reset_table_name resets the table name calculated from the prefix, suffix (and possibly other things), the table is initialized when using the model and a query is generated. ActiveRecord runs on top of Arel , relational algebra. When the ActiveRecord model is used, a table is created and the @arel_table instance variable is @arel_table . This caching is designed to improve performance. If you want to recreate the isl table, it can be reset by calling reset_column_information . I needed to have both reset_table_name and reset_column_information in order to get a new table for the new table name. I probably have to worry about performance if I reset the table often.

+8
source share

All Articles