ActiveRecord忽略table_name_prefix [英] ActiveRecord ignores table_name_prefix
问题描述
我遇到这样的情况,我需要在模型中定义table_name和table_name_prefix,并且出于某种原因table_name会覆盖table_name_prefix。
I've got situation where I need to define table_name and table_name_prefix within a model and for some reason table_name overrides table_name_prefix.
class ScheduleItem < ActiveRecord::Base
self.table_name = "schedule_item"
self.table_name_prefix = 'ACQ_IBM_T.'
end
前缀在查询中被完全忽略。但是,当我注释掉table_name部分时,会添加前缀。
The prefix is completely ignored in the queries. But, when I comment out the table_name part then the prefix is added. Anyone has seen strange behavior like this before?
推荐答案
在 ActiveRecord :: ModelSchema :: ClassMethods , table_name
设置器将值放在 @table_name
:
def table_name=(value)
...
@table_name = value
和 table_name
getter仅使用 @table_name
值(如果已定义):
And the table_name
getter just uses the @table_name
value if it is defined:
def table_name
reset_table_name unless defined?(@table_name)
@table_name
end
table_name_prefix
仅在试图根据类名猜测表名时用于帮助Rails(在 reset_table_name
)。
The table_name_prefix
is only used to help Rails when it is trying to guess the table name based on the class name (in reset_table_name
).
如果您拥有Rails不能猜测的表名,则可以执行以下操作:
If you have a table name that Rails can't guess, you can do this:
class ScheduleItem < ActiveRecord::Base
self.table_name = "ACQ_IBM_T.schedule_item"
或者如果您需要要在其他地方使用前缀,您可以执行以下操作:
Or if you need to use the prefix somewhere else, you can do this:
class ScheduleItem < ActiveRecord::Base
self.table_name_prefix = 'ACQ_IBM_T.'
self.table_name = "#{table_name_prefix}schedule_item"
这篇关于ActiveRecord忽略table_name_prefix的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!