# How can i know type of attribute with active record

**URL:** <https://www.ruby-forum.com/t/how-can-i-know-type-of-attribute-with-active-record/130832>\
**Category:** Rails\
**Created:** [March 3, 2008, 7:18pm UTC](https://www.ruby-forum.com/t/how-can-i-know-type-of-attribute-with-active-record/130832 "2008-03-03T19:18:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ricardvince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/278dde/32.png) [@ricardvince](https://www.ruby-forum.com/u/ricardvince)\
**Post date:** [March 3, 2008, 7:18pm UTC](https://www.ruby-forum.com/t/how-can-i-know-type-of-attribute-with-active-record/130832/1 "2008-03-03T19:18:01Z")

</div>

Hello,

In my database, I have a table “products” with attributes :

- id
- title -\>string
- desc -\> text

I use active record, but I don’t know why recover the type of this  
attribute with active record?

I try attributes\_before\_type\_cast but I don’t see the type

Can you help me?

Excuse for my english (I’am a young student french)

---

<div class="post-metadata">

**Author:** ![ricardvince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/278dde/32.png) [@ricardvince](https://www.ruby-forum.com/u/ricardvince)\
**Post date:** [March 4, 2008, 12:38am UTC](https://www.ruby-forum.com/t/how-can-i-know-type-of-attribute-with-active-record/130832/2 "2008-03-04T00:38:13Z")

</div>

Richard Vincent wrote:

> I try attributes\_before\_type\_cast but I don’t see the type
> 
> Can you help me?
> 
> Excuse for my english (I’am a young student french)

Either of these should get you what you want.

MyClass.columns\_hash[‘column\_name’].type

@instance\_of\_my\_class.column\_for\_attribute(:column\_name).type

Although, I should mention that in nearly two years of developing in  
Rails professionally, I don’t think I’ve ever needed to retrieve this  
information in any of my apps. Are you sure that what you really want  
isn’t the ruby class of the value (eg. @instance.column\_name.class ),  
or something else?

–  
[http://www.5valleys.com/](http://www.5valleys.com/)  
[http://www.workingwithrails.com/person/8078](http://www.workingwithrails.com/person/8078)

---

<div class="post-metadata">

**Author:** ![ricardvince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/278dde/32.png) [@ricardvince](https://www.ruby-forum.com/u/ricardvince)\
**Post date:** [March 4, 2008, 1:52pm UTC](https://www.ruby-forum.com/t/how-can-i-know-type-of-attribute-with-active-record/130832/3 "2008-03-04T13:52:56Z")

</div>

Ok thanks for your example, it’s goog for my app

Other question :  
In my database, I have a table “line\_items” with attributes :

- id → integer
- product\_id → integer  
and I have a foreign key (product-id references table product)

It is possible to recover the class of attribute product\_id?  
In my app I list object in my database (like phpMyAdmin) and if a  
foreign key I want to link to the object reference by this foreign key.

For example, in “line\_items”, list object of this table and I want a  
link in “product\_id” to this product

I don’t know if it’s possible???

Jon G. wrote:

> Richard Vincent wrote:
> 
> > I try attributes\_before\_type\_cast but I don’t see the type
> > 
> > Can you help me?
> > 
> > Excuse for my english (I’am a young student french)
> 
> Either of these should get you what you want.
> 
> MyClass.columns\_hash[‘column\_name’].type
> 
> @instance\_of\_my\_class.column\_for\_attribute(:column\_name).type
> 
> Although, I should mention that in nearly two years of developing in  
> Rails professionally, I don’t think I’ve ever needed to retrieve this  
> information in any of my apps. Are you sure that what you really want  
> isn’t the ruby class of the value (eg. @instance.column\_name.class ),  
> or something else?
> 
> –  
> [http://www.5valleys.com/](http://www.5valleys.com/)  
> [http://www.workingwithrails.com/person/8078](http://www.workingwithrails.com/person/8078)
