mercredi 29 avril 2015

Model with namespace - wrong table name (without namespace)

I found a problem in one of legacy applications (outdated rails-3.0.20). This application has lots of components and nested models. Problem existed only on one of production servers (same environments like other productions and mine dev).

There was model with name space which looks like

module Great
  class Item
  end
end

Table name was named great_items.

When i debug/open it on server with fault i've found that calculated table name was items istead of great_items.

$ Great::Item.all
#=> ActiveRecord::StatementInvalid: No attribute named `name` exists for table `items`

So i thought mby there is simmilar class with same namespace, I've checked it and it wasn't. My 2nd thought was to set table name explicit i tried

self.table_name = 'great_items'
# &
set_table_name 'great_items'

After this changes i run rails c and table name was setted fine:

$ Great::Items.table_name
#=> 'great_items'

But when i tried to obtain some items there was a freak error, which i could not understand till now!

$ Great::Items.all
#=> ActiveRecord::StatementInvalid: Mysql2::Error: Table 'db.items' doesn't exist: SELECT `great_items`.* FROM `items` WHERE `great_items`.`some_default_scope` = 0

As you can see in above example table has correct name for select values and in where statement, but in from value is incorrect.

I was curious so I've checked ActiveRecord::Base mysql adapter and there was some kind of catching table name so i tried to reset_table_name. Reset helped to setup expected name('great_items') but above errors didn't missed.

Problem dissapeared when i turn of class catching in production enviroment - but it wasn't solution.

Finally I kinda 'solved' this using reset_column_information after set_table_name, but i think it isn't good solution either.

My question is do you know what really could cause this issue and how to solve it without reloading class cache?

Aucun commentaire:

Enregistrer un commentaire