Solved

How do I find out the variable type of a certain field retrieved from the database in Rails / Ruby?

Posted on 2010-08-14
1
585 Views
Last Modified: 2013-11-24
Hi, I have an application in Ruby on Rails which I need to find out what the column type is in the database through active record.

So for example, if I have a model Contact with the fields :name string and :total integer. In the model, the defaults created_at for timestamp and id will be there.

I would like to know how to find out based on a post from a form, what the particular column is.

So ... I have a form which posts a variable field_name through to an action in a controller.

In the controller, I've got an action ...

def find_out

contact = Contact.find(:first)
field_name = params[:field_name]
# find out what the field_name's column type is in Contact model

if column_type(field_name) == 'timestamp'
# do something
elsif column_type(field_name) == 'integer'
# do something else
end

end

I hope you get the idea... it's like the mysql_field_type() in PHP.

Thanks.

end
0
Comment
Question by:binele
1 Comment
 
LVL 14

Accepted Solution

by:
wesgarrison earned 500 total points
Comment Utility
You can use script/console to play around with these things, here's a sample from one of my apps:
# For example, typing "User"
> User

# returns the class with all fields and their types
 => User(id: integer, name: string, ...)

# you can just get the Ruby class:
> User.first.name.class
 => String 


# or, maybe you need the db definition:
> User.new.column_for_attribute('name')
 => #<ActiveRecord::ConnectionAdapters::MysqlColumn:0x20c2090 @default=nil, @limit=100, @type=:string, @null=true, @scale=nil, @name="middle", @sql_type="varchar(100)", @precision=nil, @primary=false>

# use the sql_type to get the actual type:
> User.new.column_for_attribute('middle').sql_type
 => "varchar(100)" 

# all of that to say, Ruby classes are probably what you're looking for:

Contact.first.attribute(params[:field]).class

Open in new window

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

I recently rediscovered rails when I needed a holiday project and decided to build a management dashboard for the company where I work.  With it being a project done in my free time, I could focus my time on learning the basics rather than trying to…
Thanks to luck and dedication (hitting refresh every 5 seconds when the pre-orders were going live at midnight on April 10th) I managed to actually receive an Apple Watch on release day, April 24th. Which means I’ve been using it daily for just ov…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now