Yet another "NameError: uninitialized constant" problem

I’ve searched for information about this and compared what I have to the
solutions I can find, here and elsewhere. I found some good and useful
information, yet my stuff still doesn’t work. I was hoping somebody here
could spot something. I’m using rails 2.1.2 and I’m pretty new to ruby
and rails.

My goal is a fairly simple many-to-many relationship linking “modules”
and “users”. There’s 3 tables (I know the names are inconsistent - it’s
a little strange because it was an existing database that I dumped into
rails):

  • module: a module definition
  • users: the users of the system
  • module_user_map: join table linking modules and users

My data model (from schema.rb) looks in part like this:
create_table “module”, :force => true do |t|
t.string “name”, :default => “”, :null => false
t.integer “status”, :default => 0, :null => false

end

create_table “module_user_map”, :force => true do |t|
t.integer “module”, :default => 0, :null => false
t.integer “user”, :default => 0, :null => false

end

create_table “users”, :force => true do |t|
t.string “first_name”, :limit => 45
t.string “last_name”, :limit => 45
t.string “login”, :limit => 45

end

Here’s the corresponding files in app/models directory. It appears that
I could not use “module” as the model name because that is a reserved
word in rails, so I had to prepend “project_” to the “module” names.
This might be where I messed up, I don’t know.

=============== project_module.rb ===================
class ProjectModule < ActiveRecord::Base
set_table_name “module”
has_many :module_user_maps, :dependent => :delete_all
has_many :users, :through => :module_user_maps
end

=============== user.rb ===================
class User < ActiveRecord::Base
has_many :module_user_maps, :dependent => :delete_all
has_many :project_modules, :through => :module_user_maps
end

=============== module_user_maps.rb ===================
class ModuleUserMaps < ActiveRecord::Base
set_table_name “module_user_map”
belongs_to :user
belongs_to :project_module
end

So I run from the script/console (same thing happens in the server):

u=User.find(2)
=> #<User id: 2, first_name: “Site”, last_name: “User”, login: “user”,
password:
“password”, organization: 2, roles: “public”, last_modified:
“2008-11-17 12:09:
10”, last_modified_by: nil, last_login: “2008-11-17 12:10:06”, status:
1, date_c
reated: nil, address1: nil, address2: nil, city: nil, state: nil,
postal_code: n
il, email: nil>

u.project_modules
NameError: uninitialized constant User::ModuleUserMap
from
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.2/lib/active_suppo
rt/dependencies.rb:493:in `const_missing’
… etc …

u.module_user_maps
NameError: uninitialized constant User::ModuleUserMap
from
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.2/lib/active_suppo
rt/dependencies.rb:493:in const_missing' from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.2/lib/active_record /base.rb:1914:incompute_type’
… etc …

I’m hoping somebody can spot something fairly simple. This should not be
a hard thing to do. Thanks!

Hi Gary,

Gary Bisaga wrote:

=============== module_user_maps.rb ===================
class ModuleUserMaps < ActiveRecord::Base
set_table_name “module_user_map”
belongs_to :user
belongs_to :project_module
end

So I run from the script/console (same thing happens in the server):

NameError: uninitialized constant User::ModuleUserMap

I think if you change the class name to singular (and the file name)
you’ll
be good to go.

Best regards,
Bill

Hallelujah! You are a lifesaver Bill - you got me almost all the way
home. At least, now it is giving me an error message that is actually
useful in figuring out what the problem is:

ActiveRecord::StatementInvalid: Mysql::Error: #42S22Unknown column
‘module_user_
map.user_id’ in ‘where clause’: SELECT module.* FROM module INNER
JOIN mo
dule_user_map ON module.id = module_user_map.project_module_id WHERE
((modul e_user_map.user_id = 2))

So, after adding a few :foreign_key declarations like this:

class ModuleUserMap < ActiveRecord::Base
set_table_name “module_user_map”
belongs_to :user, :foreign_key => “user”
belongs_to :project_module, :foreign_key => “module”
end

we’re good. Thank you so much! It would be great if this gave better
error messages.

<>< gary

Bill W. wrote:

I think if you change the class name to singular (and the file name)
you’ll be good to go.

Hi Gary,

Gary Bisaga wrote:

we’re good. Thank you so much!

You’re very welcome. Glad to help.

Best regards,
Bill