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:inload_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