[英]Does ActiveRecord cache table_name?
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: 我有一个
ActiveRecord
类,该类需要根据配置开关查看两个不同的表,因此我打算执行以下操作:
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. 我想知道在针对该模型运行查询时是否总是调用
table_name
方法:当配置更改时,应用程序将不会重新启动,因此无法缓存该值。
Does ActiveRecord
always evaluate the table_name
or just once during application startup / initialization? ActiveRecord
总是在应用程序启动/初始化期间始终评估table_name
还是仅评估一次? If it's cached how do I force it to evaluate table_name
every time? 如果已缓存,我如何强制其每次对
table_name
求值?
Not only the table name, also the column definition. 不仅是表名,还包括列定义。 You'd better go with a schema setup like with the Apartment gem.
您最好采用与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.
因此,您可以执行两个模式,一个使用第一个表,一个使用第二个表(其余表可能只在一个模式上),然后更改搜索路径,您将获得不同的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.