# Help? Unitialized constant NameError in Model

**URL:** <https://www.ruby-forum.com/t/help-unitialized-constant-nameerror-in-model/179436>\
**Category:** Rails\
**Created:** [November 24, 2009, 10:42pm UTC](https://www.ruby-forum.com/t/help-unitialized-constant-nameerror-in-model/179436 "2009-11-24T22:42:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [November 24, 2009, 10:42pm UTC](https://www.ruby-forum.com/t/help-unitialized-constant-nameerror-in-model/179436/1 "2009-11-24T22:42:03Z")

</div>

Hello,

I am totally stumped on this one.

I have two classes sitting in the lib folder under the main application  
directory. For now, those two classes are identical except for the file  
name (attribute.rb and group.rb) and the name of the classes Attribute  
and Group respectively.

In the models directory I have a model that contains two instance  
variables. One will hold an array of Attribute objects and the other  
will contain an array of Group objects. (Note that the class doesn’t  
extent ActiveRecord::Base because I don’t need a DB right now.

That said, I can instantiate Attribute objects all day long and push  
them into the array. But it blows up when I try to create an instance  
of a Group object telling me that there is an unitialized constant  
Prod::Group (NameError)

Anyone have any idea wth I’m doing wrong? I don’t use a require  
statement for either file because it’s in the lib directory and is  
unneeded

The two classes in the lib directory look like this for now:

class Attribute  
attr\_accessor :name  
attr\_accessor :value

def initialize(name, value)  
@name = name  
@value = value  
end  
end

It’s really not anything complicated so I don’t have a clue how one  
blows up and the other doesnt!!!

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [November 25, 2009, 1:52am UTC](https://www.ruby-forum.com/t/help-unitialized-constant-nameerror-in-model/179436/2 "2009-11-25T01:52:28Z")

</div>

David Sainte-claire wrote:

> It’s really not anything complicated so I don’t have a clue how one  
> blows up and the other doesnt!!!

This must be something else to this puzzle. I created your setup on an  
new Rails application and tested in the console. Here are the results:

$ ./script/console  
Loading development environment (Rails 2.3.4)

> > att = Attribute.new(“Hello”, “World”)  
> > =\> #\<Attribute:0x1005da970 @name=“Hello”, @value=“World”\>
> 
> > gr = Group.new(“Hello”, “World”)  
> > =\> #\<Group:0x10059da48 @name=“Hello”, @value=“World”\>
> 
> > att.name  
> > =\> “Hello”
> 
> > att.value  
> > =\> “World”
> 
> > gr.name  
> > =\> “Hello”
> 
> > gr.value  
> > =\> “World”

And the classes:

class Attribute  
attr\_accessor :name  
attr\_accessor :value

def initialize(name, value)  
@name = name  
@value = value  
end  
end

class Group  
attr\_accessor :name  
attr\_accessor :value

def initialize(name, value)  
@name = name  
@value = value  
end  
end

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [November 30, 2009, 6:52pm UTC](https://www.ruby-forum.com/t/help-unitialized-constant-nameerror-in-model/179436/3 "2009-11-30T18:52:49Z")

</div>

Robert W. wrote:

> David Sainte-claire wrote:
> 
> > It’s really not anything complicated so I don’t have a clue how one  
> > blows up and the other doesnt!!!
> 
> This must be something else to this puzzle. I created your setup on an  
> new Rails application and tested in the console. Here are the results:
> 
> $ ./script/console  
> Loading development environment (Rails 2.3.4)
> 
> > > att = Attribute.new(“Hello”, “World”)  
> > > =\> #\<Attribute:0x1005da970 @name=“Hello”, @value=“World”\>
> > 
> > > gr = Group.new(“Hello”, “World”)  
> > > =\> #\<Group:0x10059da48 @name=“Hello”, @value=“World”\>
> > 
> > > att.name  
> > > =\> “Hello”
> > 
> > > att.value  
> > > =\> “World”
> > 
> > > gr.name  
> > > =\> “Hello”
> > 
> > > gr.value  
> > > =\> “World”
> 
> And the classes:
> 
> class Attribute  
> attr\_accessor :name  
> attr\_accessor :value
> 
> def initialize(name, value)  
> @name = name  
> @value = value  
> end  
> end
> 
> class Group  
> attr\_accessor :name  
> attr\_accessor :value
> 
> def initialize(name, value)  
> @name = name  
> @value = value  
> end  
> end

The probability of this happening is almost nil, but of course it had to  
happen to me! When I finally resolved it I had nearly pulled all my  
hair out. I was using REXML to parse an XML document and since I am new  
to Ruby I did require ‘rexml’ and include ‘REXML’ which imported the  
REXML namespace. What I found out later is that REXML also has an  
Attribute class within it, that has attributes with :name and :value,  
and a constructor that takes :name, and :value as arguments in the same  
order and produces the same behaviour. So my class wasn’t being used at  
all, I just didn’t know it!

I found it out by putting in a debug puts #(Attribute} and found that it  
was of type REXML/Attribute.

I can’t believe how long I spent on that before I found it. Seriously,  
that was one for the record books!

Lesson to other new programmers to Ruby, don’t include things that will  
obscure your namespace until you’re sure that you’re not stepping on  
something! It took me two days before I figured out that the code I was  
using wasn’t even the code I wrote, because it was working exactly the  
same!
