mysql - Model with namespace - wrong table name (without namespace) -
i found problem in 1 of legacy applications (outdated rails-3.0.20). application has lots of components , nested models. problem existed on 1 of production servers (same environments other productions , mine dev).
there model name space looks like
module great class item end end
table name named great_items
.
when debug/open on server fault i've found calculated table name items
istead of great_items
.
$ great::item.all #=> activerecord::statementinvalid: no attribute named `name` exists table `items`
so thought mby there simmilar class same namespace, i've checked , wasn't. 2nd thought set table name explicit tried
self.table_name = 'great_items' # & set_table_name 'great_items'
after changes run rails c
, table name setted fine:
$ great::item.table_name #=> 'great_items'
but when tried obtain items there freak error, not understand till now!
$ great::item.all #=> activerecord::statementinvalid: mysql2::error: table 'db.items' doesn't exist: select `great_items`.* `items` `great_items`.`some_default_scope` = 0
as can see in above example table has correct name select
values , in where
statement, in from
value incorrect.
i curious i've checked activerecord::base
mysql adapter , there kind of catching table name tried reset_table_name
. reset helped setup expected name('great_items') above errors didn't missed.
problem dissapeared when turn of class catching in production enviroment - wasn't solution.
finally kinda 'solved' using reset_column_information
after set_table_name
, think isn't solution either.
my question know cause issue , how solve without reloading class cache?
assumed table names dont take account modules, noticed.
but know, can set yourself, using:
self.table_name = 'great_items'
according this doc, because use 3.0.x, have use:
set_table_name "great_items"
this must put on top of class definition
Comments
Post a Comment