# Strange traits in single-inheritance

**URL:** <https://www.ruby-forum.com/t/strange-traits-in-single-inheritance/137211>\
**Category:** Rails\
**Created:** [May 10, 2008, 2:17am UTC](https://www.ruby-forum.com/t/strange-traits-in-single-inheritance/137211 "2008-05-10T02:17:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![taro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/df705f/32.png) [@taro](https://www.ruby-forum.com/u/taro)\
**Post date:** [May 10, 2008, 2:17am UTC](https://www.ruby-forum.com/t/strange-traits-in-single-inheritance/137211/1 "2008-05-10T02:17:24Z")

</div>

## I’m not sure why this is happening, but apparently, the only way I can access higher-leveled model in the single-inheritance models is by calling the lower ones. For example, in class Admin \< Person

C:\InstantRails\_2.0\rails\_apps\jsa\> ruby script\console development

> > Admin.find(:first)  
> > Admin.find(:first)  
> > NameError: uninitialized constant Admin  
> > from c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2-/lib/  
> > active\_support/dependencies.rb:266:in `load_missing_constant' from c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2-/lib/ active_support/dependencies.rb:453:in`const\_missing’  
> > from c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2-/lib/  
> > active\_support/dependencies.rb:465:in `const\_missing’  
> > from (irb):1

> > Person.find(:first)  
> > Person.find(:first)  
> > =\> #\<Admin id: 2, rin: “—”, first\_name: “—”, last\_name: “—”,  
> > email: “—”, hashed\_password: “—”, salt: “—”, created\_at:  
> > “2008-05-09 20:08:10”, updated\_at: “2008-05-09 20:08:10”, created\_on:  
> > “2008-05-09”, type: “Admin”\>

> > Admin.find(:first)  
> > Admin.find(:first)  
> > =\> #\<Admin id: 2, rin: “—”, first\_name: “—”, last\_name: “—”,  
> > email: “—”, hashed\_password: “—”, salt: “—”, created\_at:  
> > “2008-05-09 20:08:10”, updated\_at: “2008-05-09 20:08:10”, created\_on:  
> > “2008-05-09”, type: “Admin”\>

> >

* * *

## Is this a new change in rails 2.0 or is it something strange that hasn’t been solved yet? By the way, here’s what my actual model look like

class Person \< ActiveRecord::Base  
#features that has to be there  
validates\_presence\_of :first\_name  
validates\_presence\_of :last\_name  
validates\_presence\_of :rin  
validates\_presence\_of :email

#features that has to be unique  
validates\_uniqueness\_of :rin  
validates\_uniqueness\_of :email

#completely ignore the passwords  
end

class Officer \< Person  
#take care of password thingy  
validates\_length\_of :password, :minimum =\> 5  
attr\_accessor :password\_confirmation  
validates\_confirmation\_of :password

#some functions  
def validate  
errors.add\_to\_base(“Missing password”) if hashed\_password.blank?  
end

def self.authenticate(name, password)  
person = self.find\_by\_rin(name)  
if person  
expected\_password = encrypted\_password(password, person.salt)  
if person.hashed\_password!=expected\_password  
person = nil  
end  
end  
person  
end

def password  
@password  
end

def password=(pwd)  
@password = pwd  
create\_new\_salt  
self.hashed\_password = Officer.encrypted\_password(self.password,  
self.salt)  
end

#careful of private statements  
private

def self.encrypted\_password(password, salt)  
string\_to\_hash = password + “Japan is an island of interest” +  
salt  
Digest::SHA1.hexdigest(string\_to\_hash)  
end

def create\_new\_salt  
self.salt = (self.object\_id \* rand).to\_s + rand.to\_s  
end  
end

class Admin \< Officer  
#only function to be concerned about  
public

def after\_destroy  
if Admin.count.zero?  
raise “Can’t delete the last admin”  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![taro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/df705f/32.png) [@taro](https://www.ruby-forum.com/u/taro)\
**Post date:** [May 10, 2008, 6:54pm UTC](https://www.ruby-forum.com/t/strange-traits-in-single-inheritance/137211/2 "2008-05-10T18:54:05Z")

</div>

On 10 May 2008, at 01:16, Taro wrote:

> I’m not sure why this is happening, but apparently, the only way I can  
> access higher-leveled model in the single-inheritance models is by  
> calling the lower ones. For example, in class Admin \< Person  
> If you want the magic autoloading to work each class needs to be in  
> its own file.

## Fred

My ramblings: [http://www.spacevatican.org](http://www.spacevatican.org)

---

<div class="post-metadata">

**Author:** ![taro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/df705f/32.png) [@taro](https://www.ruby-forum.com/u/taro)\
**Post date:** [May 10, 2008, 7:05pm UTC](https://www.ruby-forum.com/t/strange-traits-in-single-inheritance/137211/3 "2008-05-10T19:05:50Z")

</div>

Frederick C. wrote:

> On 10 May 2008, at 01:16, Taro wrote:
> 
> > I’m not sure why this is happening, but apparently, the only way I can  
> > access higher-leveled model in the single-inheritance models is by  
> > calling the lower ones. For example, in class Admin \< Person  
> > If you want the magic autoloading to work each class needs to be in  
> > its own file.
> 
> Fred

else you can just do a require “person” before you try to use Actor,  
but really, you want them in separate files.

app/models/  
admin.rb  
officer.rb  
person.rb
