Skip to content

Bug in setting table_name when using multiple namespaces. #1335

iHiD opened this Issue May 26, 2011 · 4 comments

4 participants

iHiD commented May 26, 2011

There is a bug in the setting of table_name when using multiple namespaces.
This bug manifests only when "config.cache_classes" is set to false, i.e. the development environment.

> rails c
Loading development environment (Rails 3.1.0.rc1)
ruby-1.9.2-p180 :001 > ExamRoom::Practice::Question.table_name
=> "exam_room_questions"

> rails c test
Loading test environment (Rails 3.1.0.rc1)
ruby-1.9.2-p180 :001 > ExamRoom::Practice::Question.table_name
=> "exam_room_practice_questions"

This can be reproduced by making a sample app with three new files and one change as outlined in the following Gist:

I will try and look into this later but I thought I'd post it in case anyone with more knowledge wants to have a look...


Your directory structure is off.

ActiveSupport::Dependencies#load_missing_constant uses String#underscore to turn constants into file names. In other words, it's looking for "ExamRoom::Practice" in something like:

"ExamRoom::Practice".underscore + ".rb"
=> "exam_room/practice.rb"

It never gets loaded because it never finds it.

radar commented May 28, 2011

I agree, having a directory first called namespaces and then having the namespaces under that seems unnecessary. Best to do as @farleyknight shows.

Ruby on Rails member

Just to add that the reason it works in test and production is that all models are loaded at startup (due to load not being threadsafe) so the constant exists even though its in the wrong place in filesystem. For the record they should like this:

# app/models/exam_room.rb
module ExamRoom
  mattr_accessor :table_name_prefix
  self.table_name_prefix = 'engine_room_'

# app/models/exam_room/practice.rb
module ExamRoom::Practice
  mattr_accessor :table_name_prefix
  self.table_name_prefix = "#{parent.table_name_prefix}practice_"

# app/models/exam_room/practice/question.rb
class ExamRoom::Practice::Question < ActiveRecord::Base

# script/rails c
>> ExamRoom::Practice::Question.table_name
=> "engine_room_practice_questions"
@pixeltrix pixeltrix closed this May 28, 2011
iHiD commented May 28, 2011

Hi guys,

Thanks for your comments.

I understand your thoughts on keeping the namespaces in the models folder. However, my app has hundreds of models and it gets somewhat cluttered, so extracting those namespaces out seemed like a sensible idea as they don't actually containing any model code.

Regardless, I'm still unsure as to why the line in application.rb is not loading the modules - is this not the point of it?
config.autoload_paths += %W(#{config.root}/app/models/namespaces)

Surely that should include the files in app/models/namespaces?

@jake3030 jake3030 pushed a commit to jake3030/rails that referenced this issue Jun 28, 2011
@lifo lifo Don't leave open dangling connections in development mode. [#1335 sta…
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.