# \[ActiveRecord\] strange issue, would like to understand before I wake up

**URL:** <https://www.ruby-forum.com/t/activerecord-strange-issue-would-like-to-understand-before-i-wake-up/165036>\
**Category:** Rails\
**Created:** [April 8, 2009, 2:12pm UTC](https://www.ruby-forum.com/t/activerecord-strange-issue-would-like-to-understand-before-i-wake-up/165036 "2009-04-08T14:12:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [April 8, 2009, 2:12pm UTC](https://www.ruby-forum.com/t/activerecord-strange-issue-would-like-to-understand-before-i-wake-up/165036/1 "2009-04-08T14:12:29Z")

</div>

running 2.3.2

simple case (db table created : ‘emails’ with a :type field)

I have the following Email model

class Email \< ActiveRecord::Base  
…  
end

class RequestEmail \< Email  
…  
end

TESTING in the console :  
script/console  
Loading development environment (Rails 2.3.2)

> > Email.new  
> > =\> #\<Email id: nil, sender\_email: nil, subject: nil, content: nil,  
> > user\_id: nil, created\_at: nil, updated\_at: nil, title: “mr”,  
> > firstname: “”, lastname: “Anonymous”, type: nil\> (Note : type is  
> > nil, not “Email” … ?)
> 
> > RequestEmail.new  
> > =\> #\<RequestEmail id: nil, sender\_email: nil, subject: nil, content:  
> > nil, user\_id: nil, created\_at: nil, updated\_at: nil, title: “mr”,  
> > firstname: “”, lastname: “Anonymous”, type: “RequestEmail”\>
> 
> > exit

now TESTING again…  
script/console  
Loading development environment (Rails 2.3.2)

> > RequestEmail.new  
> > NameError: uninitialized constant RequestEmail  
> > …const\_missing

I’ll appreciate some feedback , should I buy a 2nd glass ? LOL

thanks

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [April 8, 2009, 2:39pm UTC](https://www.ruby-forum.com/t/activerecord-strange-issue-would-like-to-understand-before-i-wake-up/165036/2 "2009-04-08T14:39:28Z")

</div>

found a trcik , but I don’t understand why …

In my ApplicationController  
require\_dependency ‘email’

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [April 8, 2009, 5:09pm UTC](https://www.ruby-forum.com/t/activerecord-strange-issue-would-like-to-understand-before-i-wake-up/165036/3 "2009-04-08T17:09:40Z")

</div>

On 8 Apr 2009, at 13:11, Erwin wrote:

> =\> #\<RequestEmail id: nil, sender\_email: nil, subject: nil, content:
> 
> I’ll appreciate some feedback , should I buy a 2nd glass ? LOL

If you try an eval a constant that does not exist const\_missing is  
called (which by default raises NameError)  
Rails implements a const\_missing that tries to load the file  
containing the constant for you. Rails can’t guess where you’ve put  
your models, so this only works if you stick to the conventions:  
email.rb contains Email, FooController is in foo\_controller.rb etc.

If you use Email before RequestEmail then email.rb is loaded (which  
defines both those classes) because Rails knows to load email.rb is  
const\_missing triggers on Email. WHen you do things the other way  
round rails does not know what to do with RequestEmail since there is  
no request\_email.rb. require\_dependency works because it loads email.rb

Fred
