# How can a polymorphic object check its own class type?

**URL:** <https://www.ruby-forum.com/t/how-can-a-polymorphic-object-check-its-own-class-type/105520>\
**Category:** Rails\
**Created:** [May 30, 2007, 6:18pm UTC](https://www.ruby-forum.com/t/how-can-a-polymorphic-object-check-its-own-class-type/105520 "2007-05-30T18:18:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![helzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e19b73/32.png) [@helzer](https://www.ruby-forum.com/u/helzer)\
**Post date:** [May 30, 2007, 6:18pm UTC](https://www.ruby-forum.com/t/how-can-a-polymorphic-object-check-its-own-class-type/105520/1 "2007-05-30T18:18:56Z")

</div>

I have a polymorphic class and need to know the type from within the  
model (for validation).

> From a controller, I can check instance[:type] to get the object’s  
> type, but how do I access this attribute from within the model?

If I check ‘type’, I get a warning (and wrong value). Checking  
‘self.class’, just says ‘Class’.

Thanks,  
Amir

---

<div class="post-metadata">

**Author:** ![helzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e19b73/32.png) [@helzer](https://www.ruby-forum.com/u/helzer)\
**Post date:** [May 31, 2007, 12:33pm UTC](https://www.ruby-forum.com/t/how-can-a-polymorphic-object-check-its-own-class-type/105520/2 "2007-05-31T12:33:52Z")

</div>

helzer wrote:

> Amir
> 
> >

Not sure I’m quite understanding this correctly but hope this helps:

class ParentKlass \< ActiveRecord::Base  
end

class ChildKlass \< ParentKlass

def self.class\_details  
puts “my class is #{self}” # self here IS the class  
puts “my base\_class is #{self.base\_class}” # gets the base class  
given a class  
end

def class\_details  
puts “my class is #{self.class}” # we’re asking for details of the  
instance’s class  
puts “my base\_class is #{self.class.base\_class}” # and then the base  
class of that class  
end

end

ChildKlass.class\_details

# my class is ChildKlass

# my base\_class is ParentKlass

ChildKlass.new.class\_details

# my class is ChildKlass

# my base\_class is ParentKlass

Cheers  
Chris

## –

[http://www.autopendium.com](http://www.autopendium.com)  
Stuff about old cars

---

<div class="post-metadata">

**Author:** ![helzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e19b73/32.png) [@helzer](https://www.ruby-forum.com/u/helzer)\
**Post date:** [May 31, 2007, 1:13pm UTC](https://www.ruby-forum.com/t/how-can-a-polymorphic-object-check-its-own-class-type/105520/3 "2007-05-31T13:13:57Z")

</div>

Hi Chris,

I tried to do that, but it didn’t work for me. My mistake was, I put  
that code in the parent class. So whenever this code was called, it  
reported the name of the parent and not the child.

Now that I’ve moved each case to the relevant child #{self} reports  
the correct class name.

And, instead of writing complex class-dependent validation in the  
parent class (which is inherently wrong to do), I now have simpler  
validation in each child.

Thanks,  
Amir
