Topic: Database tables and namescaping

I have a large project that some time ago I split up in to namespaced sections, so for example, I have a model SampleLog::Sample. As it was not originally in a namespace, this uses a table called "samples".

# In app/models/sample_log/sample.rb
class SampleLog::Sample < ActiveRecord::Base
...
end

# In db/migrate/20081015123000_create_samples.rb
class CreateSamples < ActiveRecord::Migration

  def self.up

    create_table :samples do |t|

      ...
      t.timestamps

    end

  end


  def self.down

    drop_table :samples

  end

end

# In schema.rb
  create_table "samples", :force => true do |t|
    ...
  end

Rails handles that fine. A month or so ago, I moved to Rails 3, and thaty was okay too.

However, I am now adding a new section, and for consistency I am naming my database tables without the namespace. For some reason Rails is expecting the namespace for these tables (for example, it is expecting "training_log_employees" here):

# In app/models/training_log/employee.rb
class TrainingLog::Employee < ActiveRecord::Base

  
...
end

# In db/migrate/20120627082426_create_employees.rb
class CreateEmployees < ActiveRecord::Migration

  def change

    create_table :employees do |t|

      ...


      t.timestamps

    end

  end

end


# In schema.rb
  create_table "employees", :force => true do |t|
    ...
  end

The original models are still working fine. Of course, I can get around this with "self.table_name = :employees" or namespacing the tables, but I would like to know why Rails is seemingly acting inconsistently. Where does Rails get the table name from if not the model itself? What could be different in my models that is causing this?

Last edited by F2Andy (2012-07-04 03:58:22)