# ActiveRecord inheritance

**URL:** <https://www.ruby-forum.com/t/activerecord-inheritance/64956>\
**Category:** Rails\
**Created:** [August 15, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/activerecord-inheritance/64956 "2006-08-15T14:20:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![prhlava](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/71e660/32.png) [@prhlava](https://www.ruby-forum.com/u/prhlava)\
**Post date:** [August 15, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/activerecord-inheritance/64956/1 "2006-08-15T14:20:55Z")

</div>

Hello,  
I got the following issue and I do not know how to resolve it:  
#1. created parent class (AccountExtended) for some my models  
(lib/classes\_lib.rb):  
class AccountExtended \< ActiveRecord::Base  
def self.findByAccount  
end  
end

#2. created model Country:  
require ‘lib/classes\_lib’  
class Country \< AccountExtended  
end

#3. and added a test controller what shows an issue  
(view\_controller.rb):  
class ViewController \< ApplicationController  
def index  
@countries = Country.find(:all)  
end  
end

index.rhtml:

\<% @countries.each do |c| -%\> \<% end -%\>

| \<%= c.full\_name %\> |

(it is simply a skeleton)

## then I start webrick, refresh page in my browser and see countries list. After second refresh a browser’s page I get:

NameError in ViewController#index

uninitialized constant AccountExtended

|RAILS\_ROOT: script/…/config/…|

Application Trace [http://localhost:3000/view#](http://localhost:3000/view#) | Framework Trace  
[http://localhost:3000/view#](http://localhost:3000/view#) | Full Trace [http://localhost:3000/view#](http://localhost:3000/view#)

|/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active\_support/dependencies.rb:123:in `const_missing' /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:131:in `const\_missing’  
#{RAILS\_ROOT}/app/models/country.rb:2  
app/controllers/view\_controller.rb:3:in `index’|

|/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active\_support/dependencies.rb:123:in `const_missing' /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:131:in `const\_missing’  
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active\_support/dependencies.rb:140:in  
`load' /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:56:in `require\_or\_load’  
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active\_support/dependencies.rb:30:in  
`depend_on' /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:85:in `require\_dependency’  
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active\_support/dependencies.rb:98:in  
`const_missing' /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:131:in `const\_missing’  
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active\_support/dependencies.rb:133:in  
`const_missing' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:941:in `perform\_action\_without\_filters’  
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action\_controller/filters.rb:368:in  
`perform_action_without_benchmark' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:69:in `perform\_action\_without\_rescue’  
/usr/local/lib/ruby/1.8/benchmark.rb:293:in `measure' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:69:in `perform\_action\_without\_rescue’  
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action\_controller/rescue.rb:82:in  
`perform_action' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:408:in `process\_without\_filters’  
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action\_controller/filters.rb:377:in  
`process_without_session_management_support' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/session_management.rb:117:in `process’  
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/dispatcher.rb:38:in  
`dispatch' /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/webrick_server.rb:115:in `handle\_dispatch’  
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/webrick\_server.rb:81:in  
`service' /usr/local/lib/ruby/1.8/webrick/httpserver.rb:104:in `service’  
/usr/local/lib/ruby/1.8/webrick/httpserver.rb:65:in `run' /usr/local/lib/ruby/1.8/webrick/server.rb:173:in `start\_thread’  
/usr/local/lib/ruby/1.8/webrick/server.rb:162:in `start_thread' /usr/local/lib/ruby/1.8/webrick/server.rb:95:in `start’  
/usr/local/lib/ruby/1.8/webrick/server.rb:92:in `start' /usr/local/lib/ruby/1.8/webrick/server.rb:23:in `start’  
/usr/local/lib/ruby/1.8/webrick/server.rb:82:in `start' /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/webrick_server.rb:67:in `dispatch’  
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/commands/servers/webrick.rb:59  
/usr/local/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:21:in  
`require' /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:147:in `require’  
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/commands/server.rb:30  
/usr/local/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:21:in  
`require' /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:147:in `require’  
script/server:3|

|/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active\_support/dependencies.rb:123:in `const_missing' /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:131:in `const\_missing’  
#{RAILS\_ROOT}/app/models/country.rb:2  
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active\_support/dependencies.rb:140:in  
`load' /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:56:in `require\_or\_load’  
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active\_support/dependencies.rb:30:in  
`depend_on' /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:85:in `require\_dependency’  
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active\_support/dependencies.rb:98:in  
`const_missing' /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:131:in `const\_missing’  
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active\_support/dependencies.rb:133:in  
`const_missing' app/controllers/view_controller.rb:3:in `index’  
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action\_controller/base.rb:941:in  
`perform_action_without_filters' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/filters.rb:368:in `perform\_action\_without\_benchmark’  
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action\_controller/benchmarking.rb:69:in  
`perform_action_without_rescue' /usr/local/lib/ruby/1.8/benchmark.rb:293:in `measure’  
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action\_controller/benchmarking.rb:69:in  
`perform_action_without_rescue' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/rescue.rb:82:in `perform\_action’  
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action\_controller/base.rb:408:in  
`process_without_filters' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/filters.rb:377:in `process\_without\_session\_management\_support’  
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action\_controller/session\_management.rb:117:in  
`process' /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/dispatcher.rb:38:in `dispatch’  
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/webrick\_server.rb:115:in  
`handle_dispatch' /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/webrick_server.rb:81:in `service’  
/usr/local/lib/ruby/1.8/webrick/httpserver.rb:104:in `service' /usr/local/lib/ruby/1.8/webrick/httpserver.rb:65:in `run’  
/usr/local/lib/ruby/1.8/webrick/server.rb:173:in `start_thread' /usr/local/lib/ruby/1.8/webrick/server.rb:162:in `start\_thread’  
/usr/local/lib/ruby/1.8/webrick/server.rb:95:in `start' /usr/local/lib/ruby/1.8/webrick/server.rb:92:in `start’  
/usr/local/lib/ruby/1.8/webrick/server.rb:23:in `start' /usr/local/lib/ruby/1.8/webrick/server.rb:82:in `start’  
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/webrick\_server.rb:67:in  
`dispatch' /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/commands/servers/webrick.rb:59 /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in `require’  
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active\_support/dependencies.rb:147:in  
`require' /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/commands/server.rb:30 /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in `require’  
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active\_support/dependencies.rb:147:in  
`require’  
script/server:3|

## |This error occured while loading the following files: country.rb account\_extended.rb

|

Please, can anybody explain me what I do wrongly ?  
Thanks.

---

<div class="post-metadata">

**Author:** ![prhlava](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/71e660/32.png) [@prhlava](https://www.ruby-forum.com/u/prhlava)\
**Post date:** [August 15, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/activerecord-inheritance/64956/2 "2006-08-15T14:20:57Z")

</div>

just put your AR model in /app/models. at least that works for me (i  
didn’t  
try it otherwise, though)

2006/8/15, Vladimir K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> #2. created model Country:
> 
> After second refresh a browser’s page I get:  
> [http://localhost:3000/view#](http://localhost:3000/view#) | Full Trace [http://localhost:3000/view#](http://localhost:3000/view#)  
> /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active\_support/dependencies.rb:131:in  
> `const_missing' /usr/local/lib/ruby/1.8/benchmark.rb:293:in `measure’  
> /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/dispatcher.rb:38:in  
> /usr/local/lib/ruby/1.8/webrick/server.rb:92:in `start' /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/commands/server.rb:30 #{RAILS_ROOT}/app/models/country.rb:2 /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:131:in /usr/local/lib/ruby/1.8/benchmark.rb:293:in `measure’  
> /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/dispatcher.rb:38:in  
> /usr/local/lib/ruby/1.8/webrick/server.rb:92:in `start’  
> /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/commands/server.rb:30  
> |
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Michael S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

[www.stellar-legends.de](http://www.stellar-legends.de) - Weltraum-Browsergame im Alpha-Stadium

---

<div class="post-metadata">

**Author:** ![prhlava](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/71e660/32.png) [@prhlava](https://www.ruby-forum.com/u/prhlava)\
**Post date:** [August 15, 2006, 2:21pm UTC](https://www.ruby-forum.com/t/activerecord-inheritance/64956/3 "2006-08-15T14:21:00Z")

</div>

Vladimir K. wrote:

> Michael S. wrote:
> 
> > just put your AR model in /app/models. at least that works for me (i  
> > didn’t try it otherwise, though)
> 
> I have moved classes\_lib.rb to /app/models, modified ‘require’ and got  
> the same error message ☹

I have renamed classes\_lib.rb -\> app/models/account\_extended.rb and now  
it is working 🙂  
but it is very strangely…

---

<div class="post-metadata">

**Author:** ![prhlava](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/71e660/32.png) [@prhlava](https://www.ruby-forum.com/u/prhlava)\
**Post date:** [August 15, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/activerecord-inheritance/64956/4 "2006-08-15T14:20:57Z")

</div>

Michael S. wrote:

> just put your AR model in /app/models. at least that works for me (i  
> didn’t try it otherwise, though)

I have moved classes\_lib.rb to /app/models, modified ‘require’ and got  
the same error message ☹

---

<div class="post-metadata">

**Author:** ![prhlava](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/71e660/32.png) [@prhlava](https://www.ruby-forum.com/u/prhlava)\
**Post date:** [August 16, 2006, 8:01am UTC](https://www.ruby-forum.com/t/activerecord-inheritance/64956/5 "2006-08-16T08:01:36Z")

</div>

Michael S. wrote:

> now i know what went wrong…  
> rails makes some assupmtions about class- and filenames. one is: a  
> file in models camelized =\> model classname. so if the autoloader  
> encounters a unknown classname (=constant), it knows in which file it  
> finds that class. if it isnt there or has another name, it fails.  
> but when request is processed at the first time all is right: rails  
> loads necessary library (require ‘classes\_lib’) and returns a correct  
> response.  
> interestingly, why at the second time loading is not working: may be it  
> decided library had been already loaded but constant (class name) is not  
> defined and it tries to load library with default name (based on  
> constant/class name) ?

---

<div class="post-metadata">

**Author:** ![prhlava](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/71e660/32.png) [@prhlava](https://www.ruby-forum.com/u/prhlava)\
**Post date:** [August 15, 2006, 2:21pm UTC](https://www.ruby-forum.com/t/activerecord-inheritance/64956/6 "2006-08-15T14:21:03Z")

</div>

now i know what went wrong…  
rails makes some assupmtions about class- and filenames. one is: a file  
in  
models camelized =\> model classname. so if the autoloader encounters a  
unknown classname (=constant), it knows in which file it finds that  
class.  
if it isnt there or has another name, it fails.

2006/8/15, Vladimir K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > the same error message ☹
> > 
> > > ```
> > > Hello,
> > > class Country < AccountExtended
> > > index.rhtml:
> > > After second refresh a browser's page I get:
> > > <http://localhost:3000/view#>> | Framework Trace
> > > --
> > > 
> > > ```
> > 
> > * * *
> 
> Vladimir K.

–  
Michael S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

[www.stellar-legends.de](http://www.stellar-legends.de) - Weltraum-Browsergame im Alpha-Stadium
