Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Bug in setting table_name when using multiple namespaces. #1335

Closed
iHiD opened this Issue · 4 comments

4 participants

@iHiD

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"

vs
> 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:
https://gist.github.com/993158

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...

@farleyknight

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

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

@pixeltrix
Owner

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_'
end

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

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

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

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 referenced this issue from a commit in jake3030/rails
@lifo lifo Don't leave open dangling connections in development mode. [#1335 sta…
…te:resolved]
d20955f
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.