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
603 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 14

Accepted Solution

by:
wesgarrison earned 500 total points
ID: 33439044
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Scrambled Images Mac OSX 10.8.1 6 124
Deploy studio and multicast 1 90
How to copy movies from Jailbroken Apple TV 2 to Windows 10 computer 7 92
Ruby or Python 7 123
HOW TO PUT VIDEOS ON YOUR iPod This article provides step-by-step instuctions on how to to use freely-available software tools to transfer video files to your iPod [step=""]DISCLAIMERS: * Please remember that making copies of copyrighted materi…
What's a UDID? If you're involved in developing, testing, or even reviewing an iOS application that's in beta, then at some point you may need to know the UDID for any iOS devices that you'll be testing on. What's the UDID? It stands for Unique Dev…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

736 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