I’ve got an ActiveRecord class that needs to look at two different tables depending on a configuration switch, so I’m planning to do the following:
def table_name
config_is_on? ? :table1 : :table2
end
I’m wondering whether the table_name method is always called when queries are run against this model: the application is not going to be restarted when the configuration changes, so this value cannot be cached.
Does ActiveRecord always evaluate the table_name or just once during application startup / initialization? If it’s cached how do I force it to evaluate table_name every time?
Not only the table name, also the column definition. You’d better go with a schema setup like with the Apartment gem. Depending on some state of a request, the actual table search path in the database is set to something. So you can do two schemas, one with the first table, one with the second (the rest of tables may be on just one schema) and changing the search path you will get different data.